When using knockout, what is the advantage of using read-only computed observables rather than simple functions?
Take the following viewmodel constructor and html snippet, for example: ?
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>
Everything here seems to work as you'd expect, so is there a reason why I should instead use:
?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>
I notice that the documentation at http://knockoutjs.com/documentation/computedObservables.html states that "...declarative bindings are simply implemented as computed observables", so does that mean there's need for me to use them explicitly in my viewmodels?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…