Your problem is in your interceptor
.
In your write
function you need to set the underlyingObservable
to null
when the value
is an empty string (calling only underlyingObservable();
only gets its value but does not set its value to null
)
and you also need to pass value
to underlyingObservable
in every other case:
var interceptor = ko.dependentObservable({
read: underlyingObservable,
write: function (value) {
if (value != null && value.trim() == '')
underlyingObservable(null);
else
underlyingObservable(value);
}
});
Demo JSFiddle. (Open your browser's console then type something in the textbox and then clear the textbox)