function MyViewModel() {
var self = this;
this.firstName = ko.observable('Planet');
this.lastName = ko.observable('Earth');
this.computedState = ko.observable(false);
this.fullName = ko.computed({
read: function () {
console.log("READ");
return this.firstName() + " " + this.lastName();
},
write: function (value) {
console.log("WRITE");
var lastSpacePos = value.lastIndexOf(" ");
if (lastSpacePos > 0) { // Ignore values with no space character
this.firstName(value.substring(0, lastSpacePos)); // Update "firstName"
this.lastName(value.substring(lastSpacePos + 1)); // Update "lastName"
}
},
owner: this,
disposeWhen : function(){ return self.computedState(); }
});
}
ko.applyBindings(new MyViewModel());
Computed observable still trigger write even i dispose of it. Is that correct behaviour ? I dont get the reason. JSFIDDLE EXAMPLE
Disposing a computed observable will make it clean up all of its subscriptions. So, while you could still write to it, the
readfunction will not run again. Disposal does not null out the computed, it is meant to make sure that there are no lingering subscriptions.For example, in this updated sample try disposing the computed and altering the
firstNamefield.http://jsfiddle.net/rniemeyer/smwwb/13/