KnockoutJS 可观察对象
KnockoutJS Observable
我是第一次使用 Knockout,我正在为一个可观察对象而苦苦挣扎。我已经在一个视图模型中声明了它,我需要访问另一个视图模型中的值。关于如何去做的任何提示?
首先在共享范围内声明您的第一个模型,然后是第二个。如果您在第二个模型中执行类似 myfirstmodel.myobservable()
的操作,您应该会看到它并与之交互。
var myModel = whatever();
var mySndModel = whateverElse();
ko.applyBindings(myModel, document.getElementById('whatever'))
ko.applyBindings(mySndModel, document.getElementById('whateverElse'))
whateverElse
是你第二个模型的构造函数,你可以在其中调用 myModel.myObservable()
你要做的,就是继承parentViewModel
function parentViewModel() {
// Just a best practice potato here :P
var self = this;
// Initialize the childViewModel change it's 'this'
childViewModel.call(self);
self.observable = ko.observable();
}
function childViewModel() {
var self = this;
console.log(self); // It will output the parentViewModel scope
}
我是第一次使用 Knockout,我正在为一个可观察对象而苦苦挣扎。我已经在一个视图模型中声明了它,我需要访问另一个视图模型中的值。关于如何去做的任何提示?
首先在共享范围内声明您的第一个模型,然后是第二个。如果您在第二个模型中执行类似 myfirstmodel.myobservable()
的操作,您应该会看到它并与之交互。
var myModel = whatever();
var mySndModel = whateverElse();
ko.applyBindings(myModel, document.getElementById('whatever'))
ko.applyBindings(mySndModel, document.getElementById('whateverElse'))
whateverElse
是你第二个模型的构造函数,你可以在其中调用 myModel.myObservable()
你要做的,就是继承parentViewModel
function parentViewModel() {
// Just a best practice potato here :P
var self = this;
// Initialize the childViewModel change it's 'this'
childViewModel.call(self);
self.observable = ko.observable();
}
function childViewModel() {
var self = this;
console.log(self); // It will output the parentViewModel scope
}