使用带有Knockout的JQueryUI DateTimePicker

时间:2013-07-22 07:28:14

标签: jquery-ui knockout.js datetimepicker

继续 knockout js bind with datetimepicker gives an exception 我现在能够使用datetimepicker与knockout,但是我无法使用同一工具的时间选择器选项,我试过的代码嵌入到下面的jsfiddle但是抛出错误

<code>
http://jsfiddle.net/saqibshakil/scdET/
</code>

编辑后检查控制台

1 个答案:

答案 0 :(得分:3)

getDate上调用timepicker似乎未返回实际的Date

您似乎可以成功使用datetimepicker来呼叫它。所以,你的绑定看起来像:

ko.bindingHandlers.timepicker = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        //initialize timepicker with some optional options
        var options = allBindingsAccessor().timepickerOptions || {};
        $(element).timepicker(options);

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            observable($(element).datetimepicker("getDate"));
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).timepicker("destroy");
        });

    },
    //update the control when the view model changes
    update: function (element, valueAccessor) {

        var value = ko.utils.unwrapObservable(valueAccessor()),
            current = $(element).datetimepicker("getDate");

        if (value - current !== 0) {
            $(element).datetimepicker("setDate", value);
        }
    }
};

更新了示例:http://jsfiddle.net/rniemeyer/L3BNw/