3

我使用以下代码创建了一个 DataPicker 控件。

<div id="date" data-win-control="WinJS.UI.DatePicker"></div>

我想使用敲除绑定到从 WinJS 生成的 3 个选择列表。有没有办法使用淘汰声明性数据绑定来绑定到生成的选择列表?

4

2 回答 2

3
ko.bindingHandlers['winjsDatePicker'] = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var value = valueAccessor(),
            allBindings = allBindingsAccessor(),
            options = allBindings['winjsDatePickerOptions'] || {};
        options['current'] = value;

        // Flatten the options for DatePicker
        var dpOptions = {};
        ko.utils.arrayForEach([
            'calendar', 'current', 'datePattern', 'disabled', 'element',
            'maxYear', 'minYear', 'monthPattern', 'yearPattern'
        ], function (prop) {
            if (options.hasOwnProperty(prop)) {
                dpOptions[prop] = ko.utils.unwrapObservable(options[prop]);
            }
        });

        // Use jQuery to store the value-observable with the element
        $(element).data('winjsDatePickerCurrent', value);

        // Create the DatePicker, and pass it the options
        var dp = new WinJS.UI.DatePicker(element, dpOptions);

        // Wire up the onchange event
        dp.addEventListener("change", function() {
            var value = $(element).data('winjsDatePickerCurrent');
            if (ko.isWriteableObservable(value)) {
                value(dp.current);
            }
        });
    },
    update: function (element, valueAccessor, allBindingsAccessor) {
        var dp = element.winControl,
            value = valueAccessor(),
            allBindings = allBindingsAccessor(),
            options = allBindings['winjsDatePickerOptions'] || {};
        options['current'] = value;

        // Update the value-observable
        $(element).data('winjsDatePickerCurrent', value);

        // Update any properties
        ko.utils.arrayForEach([
            'calendar', 'current', 'datePattern', 'disabled', 'element',
            'maxYear', 'minYear', 'monthPattern', 'yearPattern'
        ], function (prop) {
            if (options[prop] !== undefined) {
                dp[prop] = ko.utils.unwrapObservable(options[prop]);
            }
        });
    }
};
<div data-bind="winjsDatePicker: date"></div>​
<div data-bind="winjsDatePicker: date, winjsDatePickerOptions: { minYear: 2012 }"></div>​

http://jsfiddle.net/MizardX/rs8Cd/

于 2012-12-10T22:37:52.333 回答
1

我可能会研究自定义绑定。您仍然可以为 DatePicker 使用声明性标记,但在自定义绑定中实例化 DatePicker 可能更容易。

注意:我没有机会对此进行测试,但它可能会为您指明正确的方向。另外,我认为在 WinJS 世界中,他们更喜欢你这样做,data-win-bind="winControl.current: yourprop"但如果你想使用 KO,可能有办法。

ko.bindingHandlers.winDatePicker = {
    init: function (element, valueAccessor) {
        var currentDate = valueAccessor() || {};

        // In order for this to be true, you would had to call WinJS.UI.processAll() and *then* ko.applyBindings()
        if (element.winControl) {
            element.winControl.current = currentDate;
        }

    },
    update: function (element, valueAccessor) {
        var currentDate = valueAccessor() || {};

        if (element.winControl) {
            element.winControl.current = currentDate;
        }
    }
};

/// <div data-bind="winDatePicker: myObservable" data-win-control="WinJS.UI.DatePicker" />

你可以看看我做的这个要点。但是没有任何保证!您需要WinJS.Binding.processAll在使用此之前致电:https ://gist.github.com/4183235

于 2012-12-10T21:58:31.417 回答