To help reject the record values, I used the following:
- Create a hidden observable that stores value.
- Returns a recordable computed observable based on a hidden observable.
- When something is written to the calculated observable, confirm it before accepting it.
I extended Knockout with this code:
ko.conditionedObservable = function (initialValue, condition) { var obi = ko.observable(initialValue); var computer = ko.computed({ read: function () { return obi(); }, write: function (newValue) {
Use it in an object as follows:
field = ko.conditionedObservable<number>(null, (v) => parseInt(v) > 0);
For more information, check out my Knockout Registration Notice: Reject Blog Values .
source share