var ViewModel = function () {
var self = this;
self.defaultValues = {
Id: ko.observable(16),
name: ko.observable("SUkhi"),
};
};
var model = new ViewModel();
ko.applyBindings(model);
如何通过调用函数来设置DB的默认值16和'Sukhi'。
答案 0 :(得分:1)
根据您的需要,有几种方法可以做到这一点。
示例1
var ViewModel = function () {
var self = this;
self.Id = ko.observable(16);
self.name: ko.observable("SUkhi")
};
var model = new ViewModel();
ko.applyBindings(model);
// this will create models with the default values
示例2(我最喜欢的)
var ViewModel = function (ctor) {
var self = this;
var default: {
id = 16
name: "SUkhi"
}
self.Id = ko.observable();
self.name: ko.observable()
/// if using pure JS
if(!!ctor){
for(var i in ctor){
if(ctor.hasOwnProperty("i") && self.hasOwnProperty("i"){
if(ko.isSubscribable(self[i])) { // check if it is observable
self[i](ctor[i])
}
else {
self[i] = ctor[i];
}
}
}
}
// end pure JS
/// if using jquery
$.extend(self, ctor);
// end jquery
};
var model = new ViewModel(); // or
var model = new ViewModel({ Id: 5, Name: "Whateva"})
ko.applyBindings(model);
从记忆中写出来,但它全都在那里