0

It seems that update in custom binding has stopped working (it was working in version 2.2.0).

I put alerts in events that should be triggered, and it is not working when Add button is pressed.

Can someone confirm this or give info where is the problem and what should be done?

See the working version (using KO 2.1.0) vs the broken version (using KO 2.2.0).

HTML:

<div data-bind="foreach: items, myBind: {}">
    <h3>
        <a href="#" data-bind="text: id"></a>
    </h3>
    <div data-bind="text: name"> </div> 
</div>

<button data-bind="click: add">Add Item</button>

<hr/>

JS:

ko.bindingHandlers.myBind = {
    init: function(element, valueAccessor) {
        alert('init');       
    },
    update: function(element, valueAccessor) {
        alert('update');        
    }
}

function Item(id, name) {
    this.id = ko.observable(id);
    this.name = ko.observable(name);
}

var viewModel = {
    items: ko.observableArray([
        new Item(1, "one"),
        new Item(2, "two"),
        new Item(3, "three")]),
    add: function() {
        viewModel.items.push(new Item(4, "foo"));
    }
};

ko.applyBindings(viewModel);
Jeroen
  • 60,696
  • 40
  • 206
  • 339
EddieHR
  • 153
  • 2
  • 11
  • The 2.2 behavior seems correct to me at first sight, because the `myBind` binding doesn't depend on `viewModel.items` in any way. Not sure what change in KO source triggered this though. – Jeroen Sep 13 '13 at 09:39
  • How should bind look like for that to be correct (this is not working too http://jsfiddle.net/wXubM/10/)? Thanks – EddieHR Sep 13 '13 at 09:41
  • ?! Impossible. :) I have Win 7, Chrome 29.0.1547.66 m and the /9 version is not showing alert when Add button is pressed. – EddieHR Sep 13 '13 at 10:27
  • I can confirm this bug in Chrome 29 on Win8. @Eriedor did you push the "Add" button? It's after that when there's a "missing" alert (compared to KO 2.1). – Jeroen Sep 13 '13 at 10:30
  • It is not only Chrome. Tried it in Firefox 17 and IE9 and the behavior is the same. Just wondering - everything is ok with the usage of Knockout JS? – EddieHR Sep 13 '13 at 10:33

1 Answers1

2

I think your question was answered here. You need to create a dependency on your observable array in the binding. For example:

update: function(element, valueAccessor) {
    //create a dependency, normally you would do something with 'data'
    var data = ko.utils.unwrapObservable(valueAccessor());
    alert('update');        
}

See this fiddle.

The fact that it was working in 2.1 can probably be considered as a bug.

Community
  • 1
  • 1
Jeroen
  • 60,696
  • 40
  • 206
  • 339