當使用淘汰賽,什麼是使用只讀觀測計算,而不是簡單的功能的優勢呢?淘汰賽:計算觀察到VS功能
看看下面的視圖模型構造和HTML片段,例如:這裏
var ViewModel = function(){
var self = this;
self.someProperty = ko.observable("abc");
self.anotherProperty = ko.observable("xyz");
self.someComputedProperty = function(){
return self.someProperty() + self.anotherProperty();
};
};
<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty()"></p>
似乎一切都如你所期望的工作,所以是有原因,我應該使用:
var ViewModel = function(){
var self = this;
self.someProperty = ko.observable("abc");
self.anotherProperty = ko.observable("xyz");
self.someComputedProperty = ko.computed(function(){
return self.someProperty() + self.anotherProperty();
});
};
<input data-bind="value: someProperty"/>
<input data-bind="value: anotherProperty"/>
<p data-bind="text: someComputedProperty"></p>
我注意到,在http://knockoutjs.com/documentation/computedObservables.html文檔指出「......聲明綁定簡單地計算觀測實施」,這是否意味着有需要我在我的ViewModels明確地使用它們?
大回答:謝謝! – Duncan 2012-07-17 19:04:31
很好的答案。當我想要每次執行完整的邏輯時,我有時會使用函數而不是計算。 – 2012-07-17 22:06:22
它是如何知道一個計算的觀察值取決於另一個可觀察值,當我通過它時是一個函數?它是否解析該功能!? - 時間來讀取淘汰賽的源代碼我猜 – zod 2013-10-10 15:06:17