Reputation: 813
I just recently picked up KO and want to do something similar to the following.
There are two inputs and they change one another.
HTML:
<input data-bind="value: var1" type="text">
<input data-bind-"value: var2" type="text">
Script:
function DataViewModel() {
var self = this;
self.price = 5;
self.var1 = ko.observable(0);
self.var2 = ko.computed({
read: function () {
if(self.var1() <= 0) {
return 0;
}
return (self.var1 - 1) / self.price;
},
write: function (value) {
self.var1((Number(value) + 1) * self.price);
// Value = 7.8, output is 44 for var1,
// var2 changes to 8.6. Desired is 40 for var1 and 7.8 for var2.
},
owner: self
});
}
ko.applyBindings(new DataViewModel());
When 40 is inputed into var1, var2 shows 7.8. But when 7.8 is inputed into var2, var1 updates to 44 instead of 40 and the var2 changes to 8.6.
I want var2 to remain what was inputed instead of undergoing the read function.
http://jsfiddle.net/tyq7ejdh/2/
Upvotes: 0
Views: 55