Binding provider function getBindingAccessors called multiple times

41 views
Skip to first unread message

Anders Malmgren

unread,
Jul 8, 2014, 12:45:58 PM7/8/14
to knock...@googlegroups.com
I have found something strange while refactoring one of my libraries which has a custom binding provider.
From what I understand getBindingAccessors should only be called once per DOM element since 3.0 (Its was a bug in 2.x that called the function each time a observable mutated).

In one of my unit tests the function get called multiple times, I have investigated why this happens and it only shows if you apply bindings directly to a observable array and push rows to the array after binding. The getBindingAccessors function will get called for each push to the array.

Michael Best

unread,
Jul 10, 2014, 6:11:03 AM7/10/14
to knock...@googlegroups.com
The the view model is an observable, Knockout must re-parse all bindings when that observable changes. That's because bindings are parsed relative to the (unwrapped) value held by the observable. In your example, $data refers to the actual array held by the observableArray. When using push, the array instance remains the same, and so getBindingAccessors doesn't need to be called again. But Knockout can't assume anything like that. Suppose that instead of using push, you had replaced the array instance, like arr(["test"]); arr(["test","tva"]);. The foreach binding will need to get the new array instance value, and the only way to do so is to re-evaluate the binding, which is what getBindingAccessors does.

-- Michael
Reply all
Reply to author
Forward
0 new messages