JSFiddle - React, Tailwind, and code Playground

by nemesv

HTML

<p>Open the console, and type something here click somewhere else and clear the textbox and click somewhere else:</p>
<input type="text" data-bind="stringEmptyNull: Ip" />

JavaScript

ko.bindingHandlers.stringEmptyNull = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            var underlyingObservable = valueAccessor();
            var interceptor = ko.dependentObservable({
                read: underlyingObservable,
                write: function (value) {
                    if (value != null && value.trim() == '')
                        underlyingObservable(null);
                    else
                        underlyingObservable(value);
                }
            });
            ko.bindingHandlers.value.init(element, function () { return interceptor }, allBindingsAccessor);
        },
        update: ko.bindingHandlers.value.update
    };

var Model = function () {
        this.Ip = ko.observable()
        ko.applyBindings(this);
    };

    instance = new Model();
instance.Ip.subscribe(function(value) {
    console.log(value);
});