0

I have just come across a problem which is quite bizarre, where I have multiple applyBindings() but they all target different elements. Now this all used to work fine, as it would all happen once all resources were loaded (a yepnope complete callback), however now its happening based on an event being raised (from our internal event system) it seems to have a weird error (saying view model does not have observable, but its because its not looking in the right one).

The issue is that the last binding seems to for some reason overwrite all other bindings, so if I had 4 elements with unique ids and 4 view models each which are bound to their coresponding element, so I would expect the bindings to occur like so:

element_a -> view_model_a
element_b -> view_model_b
element_c -> view_model_c
element_d -> view_model_d

However for some reason the bindings seem to change when view_model_d is bound so they all become:

element_a -> view_model_d
element_b -> view_model_d
element_c -> view_model_d
element_d -> view_model_d

As I said this used to work fine, but since we turned to an asynchronous way of applying the bindings it seems to be a bit broken, all of the bound elements are in the html and self contained (i.e none of them are dynamically injected and dont overlap in the DOM). Also when debugging in firebug the viewmodels and the elements are correct and not undefined, which could have explained why the last one called would overwrite all others.

I would try and do a jsfiddle but due to the complex nature of the project and the external templates and dependencies it would not be possible.

All things point to me doing something wrong, not KnockoutJS, but I cannot understand why it used to work fine the only thing that has changed is the order and timing of the apply bindings.

Anyone ever seen applyBindings bleeding over into other elements or overwriting previous bindings before?

4

1 回答 1

0

像往常一样,答案归结为我是个白痴。由于我使用了一个使用 templateUrl 作为其选项之一的外部模板加载器,出于某种原因,它是 templateurl,由于某种原因,它最初必须有效,但由于现在加载的顺序,它正在重新加载同一页面,不是所需的模板,这意味着将重新加载整个页面绑定:(

于 2012-07-09T10:32:28.967 回答