0

I have that a custom binding that fires for each element on the page that uses the same binding ie. this code will fire the binding 3 times when I enter text in the first field.

<input data-bind="pinTest: propertyOne" />
<input data-bind="pinTest: propertyTwo" />
<input data-bind="pinTest: propertyThree" />

How do I get it to only fire once? I've tried knockout v2.3 and v3.

I've got a working example of the issue at http://jsbin.com/UKewOvu/2/edit?html,js,console,output

Here's the code:

ko.bindingHandlers.pinTest = {
  init: function (element, valueAccessor, allBindingsAccessor) {

    ko.utils.registerEventHandler(element, "keyup", function () {
      var value = valueAccessor();
      value(element.value);
    });
  },
  update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
    var value = ko.unwrap(bindingContext.$data.propertyOne);
    console.log("in binding update -value="+value);
  }
};

var myVm = function () {

  this.propertyOne = ko.observable();
  this.propertyTwo = ko.observable();  
  this.propertyThree = ko.observable();
};

function start() {
  ko.applyBindings(new myVm());
}
JotaBe
  • 38,030
  • 8
  • 98
  • 117
dex
  • 275
  • 1
  • 4
  • 10

1 Answers1

1

var value = ko.unwrap(bindingContext.$data.propertyOne) will create a dependecy to propertyOne for all 3 properties

Instead do

console.log("in binding update -value="+ ko.unwrap(valueAccessor()));
Anders
  • 17,306
  • 10
  • 76
  • 144