I have this (very simplified) HTML:
<label>Quantity: <input name="quantity" type="number" data-bind="textInput: quantity"/></label>
<label>Euros: <input name="wholeNumber" type="number" data-bind="textInput: wholeNumber"/></label>
And this (also very simplified) Knockout model:
function ViewModel() {
self = this;
cents = ko.observable();
quantity = ko.observable();
wholeNumber = ko.computed({
read: function() {
return (self.cents() / 100).toFixed(2);
},
write: function(value) {
self.cents(Math.round(value * 100));
}
});
}
ko.applyBindings(ViewModel);
See this fiddle for a running example: https://jsfiddle.net/KHFn8/5569/
When I run this code the value of the quantity does not refresh while typing, but the value of the wholeNumber does change after each keystroke.
I have two questions about this:
- Since both fields have a two way binding why does this only happen in the case of the computed observable (wholeNumber), but not in the case of a normal observable?
- How can I prevent the updating of the field from happening while I'm still typing. I can use
wholeNumber.extend({ rateLimit: { timeout: 500, method: "notifyWhenChangesStop" } });
, but that's an suboptimal solution.