我试图从我的视图模型中绑定kendoDatePicker值。
当我使用简单的可观察时,它完美地工作。 HTML:
<input data-bind="kendoDatePicker: { value: myDate().selectedDate} " />
更改日期
var DateModel = function (json) {
var self = this;
self.selectedDate = ko.observable((!json || !json.Value) ? new Date('01/02/2015') : new Date(json.Value));
};
function ViewModel() {
var self = this;
// initializing my object with empty string to avoid html databinding exceptions
self.myDate= ko.observable(new DateModel(''));
self.changeDate = function () {
self.myDate(new DateModel({Value:"09/08/1935"}));
};
return self;
};
ko.applyBindings(new ViewModel());
<!-- REFERENCES
http://cdn.kendostatic.com/2012.3.1315/styles/kendo.common.min.css
http://cdn.kendostatic.com/2012.3.1315/styles/kendo.default.min.css
http://cdn.kendostatic.com/2012.3.1315/js/kendo.core.min.js
http://cdn.kendostatic.com/2012.3.1315/js/kendo.web.min.js
http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js
http://rniemeyer.github.com/knockout-kendo/js/knockout-kendo.min.js
-->
<input data-bind="kendoDatePicker: { value: myDate().selectedDate} " />
<button data-bind="click: changeDate">Change Date</button>
<p data-bind="text: myDate().selectedDate"> </p>
演示:http://jsfiddle.net/NGLEp/27/
但是当我使用带有json值的可观察模型时(比如我解析我的web服务结果)它不起作用。
var DateModel = function (json) {
var self = this;
self.selectedDate = ko.observable((!json || !json.Value) ? new Date('01/02/2015') : new Date(json.Value));
};
function ViewModel() {
var self = this;
// initializing my object with empty string to avoid html databinding exceptions
self.myDate= ko.observable(new DateModel(''));
self.changeDate = function () {
self.myDate().selectedDate(new Date());
};
return self;
};
ko.applyBindings(new ViewModel());
<!-- REFERENCES
http://cdn.kendostatic.com/2012.3.1315/styles/kendo.common.min.css
http://cdn.kendostatic.com/2012.3.1315/styles/kendo.default.min.css
http://cdn.kendostatic.com/2012.3.1315/js/kendo.core.min.js
http://cdn.kendostatic.com/2012.3.1315/js/kendo.web.min.js
http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js
http://rniemeyer.github.com/knockout-kendo/js/knockout-kendo.min.js
-->
<input data-bind="kendoDatePicker: { value: myDate().selectedDate} " />
<button data-bind="click: changeDate">Change Date</button>
<p data-bind="text: myDate().selectedDate"> </p>
请帮忙!
答案 0 :(得分:0)
这个
self.myDate(new DateModel({Value:"09/08/1935"}));
覆盖与datePicker控件绑定的旧selectedDate
实例。您可以使用setter
方法设置最初绑定的可观察值。
self.setDate = function(json){
self.selectedDate((!json || !json.Value) ? new Date('01/02/2015') : new Date(json.Value));
};