我有三个可观察者
viewModel.a = ko.observable(0);
viewModel.b = ko.observable(0);
viewModel.c = ko.observable(0);
我在计算的可观察的
中需要它们viewModel.co = ko.computed(function(){
var a = this.a();
return this.b() + this.c();
}, viewModel);
如果co
更新b
或c
,如何更新{{1}}?
http://jsfiddle.net/DgVCF/
答案 0 :(得分:3)
您可以使用peek()
函数获取a
的值,而不会在计算中创建对它的依赖:
viewModel.co = ko.computed(function(){
var a = this.a.peek();
console.log(1);
return this.b() + this.c();
}, viewModel);
演示JSFiddle。
答案 1 :(得分:0)
似乎更像是订阅
的工作var viewModel = {};
viewModel.a = ko.observable(0);
viewModel.b = ko.observable(0);
viewModel.c = ko.observable(0);
viewModel.co = ko.observable(0);
viewModel.b.subscribe(function(newValue) {
viewModel.co(parseInt(viewModel.b()) + parseInt(viewModel.c()));
});
viewModel.c.subscribe(function(newValue) {
viewModel.co(parseInt(viewModel.b()) + parseInt(viewModel.c()));
});
ko.applyBindings(viewModel);