JavaScript对象没有任何办法能引用他们自身,所以你需要通过myViewModelObject.myDependentObservable = ... 的形式添加依赖监控属性到view model对象上。 你不能直接在view model里声明他们,换句话说,你不能写成下面这样:
varviewModel = {
myDependentObservable: ko.dependentObservable(function() {
...
},/*can't refer to viewModel from here, sothis doesn't work*/)
}
… 相反你必须写成如下这样:
varviewModel = {
//Add other properties here as you wish
};
viewModel.myDependentObservable = ko.dependentObservable(function() {
...
}, viewModel);//This is OK