Search code examples
knockout.jsknockout-mapping-pluginko-custom-binding

custom binding handler not firing


just trying to create my first custom binding handler:

<input data-bind="value:firstName,valueUpdate:'afterkeydown'" type="text" />
<button data-bind="myhandler: firstName" ></button>

ko.bindingHandlers.myhandler =
    {
        update: function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor);
            $(element).css("background", "red");
            console.log('update');
        }
    }

var vm = function () {
    this.firstName = ko.observable('bert');
}

ko.applyBindings(new vm());

the thing is that when I update the value it does not trigger the 'update'. I only gets triggered at startup? here is the jsfiddle link: http://jsfiddle.net/dingen2010/c43hu/2/


Solution

  • You need to use the getter on the value accessor to subscribe to the value changes -

    http://jsfiddle.net/c43hu/3/

    var value = ko.utils.unwrapObservable(valueAccessor());