将xdan / datetimepicker从2.4.0更新到2.5.4后 - $ .datetimepicker未定义

时间:2016-09-29 06:55:17

标签: knockout.js gulp datetimepicker browserify-shim

以前我用过" xdan / datetimepicker" V2.4.0。但是我需要升级它,升级之后它就可以了。 它匆匆而过:

vendor.js:20065 Uncaught TypeError: Unable to process binding "with: function (){return $root.manageView }"
Message: Unable to process binding "datetimepicker: function (){return timeFrom }"
Message: $element.datetimepicker is not a function

有我的" package.json"文件:

{  
"name": "App1",
  "version": "1.0.0",
  "description": "App1 Testing",
  "main": "./src/app/app.js",
  "scripts": {
    "test": ""
  },
  "browserify": {
    "transform": [
      "browserify-shim",
      "debowerify"
    ]
  },
  "browser": {
    "bootstrap": "./node_modules/bootstrap-sass/assets/javascripts/bootstrap.js",
    "jquery.datetimepicker": "./node_modules/jquery-datetimepicker/jquery.datetimepicker.js",
    "jquery.xdomainrequest": "./src/vendor/jquery.xdomainrequest.min.js",
    "datatables": "./src/vendor/datatables/jquery.dataTables.min.js",
    "datatables.bootstrap": "./src/vendor/datatables/dataTables.bootstrap.js",
    "datatables.scroller": "./src/vendor/datatables/dataTables.scroller.min.js",
    "spectrum": "./src/vendor/spectrum/spectrum.js",
    "stringformat": "./src/vendor/stringformat-1.09.min.js",        
    "jquery.fileDownload": "./src/vendor/jquery.fileDownload.js"
  },
  "browserify-shim": {        
    "translations": "global:translations",
    "jquery-datetimepicker": { "depends": [ "jquery:$" ] }
  },
  "author": "XXX",
  "license": "ISC",
  "devDependencies": {
    "bootstrap-sass": "^3.3.6",
    "browserify": "^13.0.0",
    "browserify-shim": "^3.8.12",
    "colors": "^1.1.2",
    "debowerify": "^1.3.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-replace": "^0.5.4",
    "gulp-ruby-sass": "^2.0.6",
    "gulp-sass": "^2.3.2",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^1.5.3",        
    "jquery.cookie": "^1.4.1",        
    "lodash": "^4.6.1",
    "moment": "^2.11.2",
    "phantomjs-prebuilt": "^2.1.4",
    "typescript": "^2.0.3",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.7.0"
  },
  "dependencies": {        
    "factor-bundle": "^2.5.0",
    "jquery": "^2.2.1",
    "jquery-datetimepicker": "2.5.4",
    "knockout": "^3.4.0",        
  }
}

" vendor.js"档案:

// jQuery plugins
require('jquery.cookie');        
require('jquery.datetimepicker');
require('jquery.fileDownload');   
require('spectrum')(window.jQuery);     // Shimmed
require('bootstrap');                   // Shimmed

// Exposing DataTable for it's plugins to work
window.DataTable = require('datatables');   // Shimmed
require('datatables.scroller');             // Shimmed    

// Enables String.format functionality
require('stringformat');                    // Shimmed

// Other vendors scripts
// Loaded here so that they won't appear in the
// main app bundle
require('knockout');    
require('moment');

这是knockoutBinding.js:

/* Date picker value binder for knockout */
    (function (ko) {
        ko.bindingHandlers.datetimepicker = {
            init: function (element, valueAccessor, allBindingsAccessor) {
                var $element = $(element);
                var value = valueAccessor(), allBindings = allBindingsAccessor();
                var valueUnwrapped = ko.utils.unwrapObservable(value);
                var options = allBindings.dtPickerOptions || {};    
                **$element.datetimepicker({**
                    step: 15,
                    format: config.timeFormat,
                    formatTime: config.timeFormatHours,
                    formatDate: config.timeFormatDate,                    
                    onChangeDateTime: function (dp, $input) {
                        var date = moment($input.val(), config.timeFormat);
                        date.set('second', 0);
                        var observable = valueAccessor();
                        observable(date.format(config.timeFormat));
                    },
                    lang: 'custom',
                    i18n : {
                        custom: translationController.datePicker
                    }                
                });

            },
            update: function (element, valueAccessor) {
                var valueUnwrapped = ko.utils.unwrapObservable(valueAccessor());
                var obs = valueAccessor();
                $(element).val(moment(valueUnwrapped, config.timeFormat).format(config.timeFormat));
            }
        };
    })(ko);

那么这是什么呢。为什么找不到$ element.datetimepicker?

1 个答案:

答案 0 :(得分:0)

找到解决方案 - 它对我有用:

<强> paskage.json:

"browser": {
...
"jquery.datetimepicker": "./node_modules/jquery-datetimepicker/jquery.datetimepicker.min.js"
},
"browserify-shim": {
...
 "jquery.datetimepicker": { "depends": [ "jquery:$" ] }
},
"dependencies": {
    ...
    "jquery": "^2.2.4",
    "jquery-datetimepicker": "2.5.4",
}

注意:不要使用非缩小版本(&#34; jquery.datetimepicker.js&#34;),因为它有bug described here

<强> gulpfile.js

var externalVendorModules = ['jquery', 'jquery.datetimepicker','moment'];

<强> vendors.js

window.jQuery = require('jquery');
require('jquery.datetimepicker')(window.jQuery);