Is There A Way To Tell Knockout To Wait To Recalculate Computed Values Until After The View Model Is Defined?
I have a complex view model which is a couple hundred lines of javascript code with a good amount of observable properties, computed observable properties, writable computed observ
Solution 1:
Computed observables accept a deferEvaluation
option that prevents the initial evaluation from happening until something actually tries to retrieve the computed's value.
You would define it like:
self.fullName = ko.computed({
read: function() {
return self.firstName() + " " + self.lastName();
},
deferEvaluation: true
});
Just for completeness, you could also specify it like:
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this, { deferEvaluation: true });
Or you could wrap it like:
ko.deferredComputed = function(evaluatorOrOptions, target, options) {
options = options || {};
if (typeof evaluatorOrOptions == "object") {
evaluatorOrOptions.deferEvaluation = true;
} else {
options.deferEvaluation = true;
}
return ko.computed(evaluatorOrOptions, target, options);
};
Post a Comment for "Is There A Way To Tell Knockout To Wait To Recalculate Computed Values Until After The View Model Is Defined?"