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
I finally solved mine by returning { controlsDescendantBindings: true }
in the init
function of the binding handler. See this
I was getting the same error in IE7/IE8. Worked fine in all other browsers including IE9/IE10.
What I found worked for me was eliminating self closing tags.
Bad
<div>
<span data-bind="text: name"/>
</div>
Good
<div>
<span data-bind="text: name"></span>
</div>
In my case, I was adding to a non existing element, or, I was adding bindings to an element that maybe exists, but it's parent did not. Similar to this:
var segDiv = $("#segments"); //did not exist, wrong id
var theDiv = segDiv.html("<div></div>");
ko.applyBindings(someVM, theDiv);
As far as I can tell, this error seems a bit overloaded in the sense that it will fire on a lot of different errors that can happen with the element, like it not existing. As such, the error description can be highly deceptive. It should have probably read:
"Failure to bind bindings to element. Possible reasons include: multiple binding attempts, element not existing, element not in DOM hierarchy, quirks in browsers, etc"
You just have to remove the bindings before you use 'applyBindings' again.
ko.cleanNode($element[0]);
should do the trick. HTH.
I had this error occur for a different reason.
I created a template for save/cancel buttons that I wanted to appear at top and bottom of the page. It worked at first when I had my template defined inside of a <script type="text/html"> element.... but then I heard you could optionally create a template out of an ordinary DIV element instead.
(This worked better for me since I was using ASP.NET MVC and none of my @variableName Razor syntax was being executed at runtime from inside of the script element. So by changing to a DIV instead, I could still have the MVC Razor engine generate HTML inside my KnockoutJs template when the page loads.)
After I changed my template to use a DIV instead of a SCRIPT element, my code looked like this.... which worked fine on IE10. However, later when I tested it on IE8, it threw that....
"You cannot apply bindings multiple times to the same element" error.....
HTML
<div id="mainKnockoutDiv" class="measurementsDivContents hourlyMeasurements">
<div id="saveButtons_template" style="display: none;">
... my template content here ...
</div>
<!--ko template: { name: 'saveButtons_template' } -->
<!--/ko-->
Some_HTML_content_here....
<!--ko template: { name: 'saveButtons_template' } -->
<!--/ko-->
</div>
JavaScript
ko.applyBindings(viewModel, document.getElementById('mainKnockoutDiv'));
THE SOLUTION:
All I had to do was move my saveButtons_template DIV down to the bottom, so that it was outside of the mainKnockoutDiv. This solved the problem for me.
I suppose KnockoutJs was trying to bind my template DIV multiple times since it was located inside the applyBindings target area... and was not using the SCRIPT element.... and was being referenced as a template.
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.