Data binding 自定义剑道小部件不';更新视图模型
我正在尝试自定义和扩展datepicker。 首先,我通过customValue扩展了活页夹:Data binding 自定义剑道小部件不';更新视图模型,data-binding,kendo-ui,kendo-mvvm,kendo-datepicker,Data Binding,Kendo Ui,Kendo Mvvm,Kendo Datepicker,我正在尝试自定义和扩展datepicker。 首先,我通过customValue扩展了活页夹: kendo.data.binders.widget.customValue = kendo.data.Binder.extend({ init: function (element, bindings, options) { kendo.data.Binder.fn.init.call(this, element, bindings, options); },
kendo.data.binders.widget.customValue = kendo.data.Binder.extend({
init: function (element, bindings, options) {
kendo.data.Binder.fn.init.call(this, element, bindings, options);
},
refresh: function(e){
var path = this.bindings.customValue.path,
source = this.bindings.customValue.source,
value = source.get(path);
this.element.value(value);
},
change: function(e){
// this method is not triggered
debugger;
}
});
然后我扩展了DatePicker小部件:
CustomDatePicker = kendo.ui.DatePicker.extend({
init: function(element, options) {
kendo.ui.DatePicker.prototype.init.call(this, element, options);
this.bind('change', this.onChange);
},
options: {
name: "CustomDatePicker",
},
onChange: function(e){
debugger;
},
});
kendo.ui.plugin(CustomDatePicker);
当视图模型更改时,会触发自定义活页夹的“刷新”方法,因此数据可以从视图模型流向小部件。它工作得很好。
但是我在从小部件绑定到视图模型(反向流)时遇到了问题。
起初,我认为datepicker中的更改会触发“customValue”活页夹中的“change”方法,但事实并非如此。
CustomDatePicker.onChange方法已触发,但其中的视图模型超出范围,因此无法设置视图模型。
我的问题是,当小部件的值发生变化时,如何更新视图模型?
谢谢你的建议
仅用于插图,小部件初始化如下:
<input
data-role="datepickercustom"
data-bind="customValue: data"
data-format="dd.MM.yyyy" />
剑道不会自动设置对绑定中更改函数的调用。您需要自己将更改函数绑定到小部件的更改事件
kendo.data.binders.widget.customValue = kendo.data.Binder.extend({
init: function (widget, bindings, options) {
kendo.data.Binder.fn.init.call(this, widget.element[0], bindings, options);
var that = this;
$(widget.element).on('change', function () {
that.change();
});
},
.
.
.
change: function() {
// this method will now be triggered
debugger;
}
});
确保遵循小部件绑定模式(小部件、绑定、选项),而不是(元素、绑定、选项)
似乎不需要扩展DatePicker小部件,除非需要实现与更新视图模型不同的其他行为