如何在Durandal框架内将Bootstrap datepicker与Knockout绑定

时间:2014-12-05 21:45:01

标签: twitter-bootstrap knockout.js datepicker durandal

我想避免把这个问题归结为existing question

所以我试图使用那个JSFiddle,但是在Durandal框架中我使用require绑定配置了main.js(请注意observable:true):

define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'modules/logger', 'modules/bindings', 'modules/hubService'], function (system, app, viewLocator, logger, bindings, hubService) {

app.configurePlugins({
    router: true,
    dialog: true,
    widget: true,
    observable: true
});

我已经使用复合绑定处理程序设置了bindings.js,如下所示:

 composition.addBindingHandler('datePicker', {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var unwrap = ko.utils.unwrapObservable;
        var dataSource = valueAccessor();
        var binding = allBindingsAccessor();
        var options = {
            keyboardNavigation: true,
            todayHighlight: true,
            autoclose: true,               
            format: 'mm/dd/yyyy'
        };
        if (binding.datePickerOptions) {
            options = $.extend(options, binding.datePickerOptions);
        }
        $(element).datepicker(options);
        $(element).datepicker('update', dataSource);
        $(element).on("changeDate", function (ev) {
            var observable = valueAccessor();
            if ($(element).is(':focus')) {
                // Don't update while the user is in the field...
                // Instead, handle focus loss
                $(element).one('blur', function (ev) {
                    var dateVal = $(element).datepicker("getDate");
                    observable(dateVal);

                });
            }
            else {
                observable(ev.date);

            }
        });
        //handle removing an element from the dom
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).datepicker('remove');
        });
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).datepicker('update', value);
    }
});

虽然这在JSFiddle中有效,但在Durandal框架中使用代码时会抛出错误。

View出现了,我可以使用datepicker,但它会在以下行引发错误:

 observable(dateVal);

任何人都知道如何使用Durandal对可观察物的使用?

0 个答案:

没有答案