Prevent the possibility of observation in certain cases
Is it possible to overload the installer on an observable so that in some cases not set its value? Say the function that fires before setting the observable, for example:
function(value) {
if (value === 'ok') {
proceed; //set value of observable
} else {
break; //do not set observable
}
}
I am guessing this can be achieved with a subscription, but I am not quite clear on how to do this.
Not that I was using Knockout 3.0.
+3
source to share
1 answer
You can use a writable computed observable with functions read
and write
:
var _prop = ko.observable();
var prop = ko.computed({
read: function() {
return _prop();
},
write: function(value ) {
if (value === 'ok') {
_prop(value); //set value of observable
}
// else do not set observable
}
});
+6
source to share