KnockoutJs v2.3.0 : Error You cannot apply bindings multiple times to the same element

后端 未结 13 1154
闹比i
闹比i 2020-12-04 17:38

I\'ve just upgraded to 2.3.0 and now I\'m getting the error

You cannot apply bindings multiple times to the same element.

that

13条回答
  •  一向
    一向 (楼主)
    2020-12-04 18:11

    If you are reusing an element over and over (A bootstrap modal dialog in my case), then calling ko.applyBindings(el) multiple times will cause this problem.

    Instead just do it once like this:

    if (!applied) {
        ko.applyBindings(el);
        applied = true;
    }
    

    Or like this:

    var apply = function (viewModel, containerElement) {
        ko.applyBindings(viewModel, containerElement);
        apply = function() {}; // only allow this function to be called once.
    }
    

    PS: This might happen more often to you if you use the mapping plugin and convert your JSON data to observables.

提交回复
热议问题