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());
即使我将其处理掉,计算出的 observable 仍然会触发写入。这是正确的行为吗?我不明白原因。 JSFIDDLE 示例