0

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/

JotaBe
  • 38,030
  • 8
  • 98
  • 117
user603007
  • 11,416
  • 39
  • 104
  • 168

1 Answers1

2

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());
PW Kad
  • 14,953
  • 7
  • 49
  • 82