Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to force applyBindings in Knockout JS
    text
    copied!<p>I want to refresh model binding for my page. As says documentation I use applyBindings method. Also I need to use few viewModels on a page. To do it I use object-wrapper + "with" keyword on binding:</p> <pre><code>ko.applyBindings({ model1: {...}, model2: {...} }); &lt;div data-bind="with: $root.model1"&gt; ... &lt;/div&gt; &lt;div data-bind="with: $root.model2"&gt; ... &lt;/div&gt; </code></pre> <p>It works fine until I don't use dynamic bindings update. If I try to add additional viewmodels on a page, it doesn't work :(</p> <p>Few pieces of code:</p> <p><strong>1) Works fine:</strong></p> <pre><code>$("#b1").click(function () { ko.applyBindings({ myModel1: { MyText: ko.observable("Some text") }, myModel2: { MyText: ko.observable("Some text") } }); </code></pre> <p>});</p> <pre><code>$("#b2").click(function () { ko.applyBindings({ myModel1: { MyText: ko.observable("Some other text") }, myModel2: { MyText: ko.observable("Some other text 2") } }); }); </code></pre> <p><strong>2) Doesn't work:</strong></p> <pre><code>$("#b1").click(function () { ko.applyBindings({ myModel1: { MyText: ko.observable("Some text") } }); </code></pre> <p>});</p> <pre><code>$("#b2").click(function () { ko.applyBindings({ myModel1: { MyText: ko.observable("Some other text") }, myModel2: { MyText: ko.observable("Some other text 2") } }); }); </code></pre> <p>As I understand, we have no way to bind additional view models in such manner. I also tried to use "ko.cleanNode()" method, but it doesn't work too. Full sample of code stored at <a href="http://jsfiddle.net/eN8dq/2/" rel="nofollow">http://jsfiddle.net/eN8dq/2/</a></p> <p>Can you suggest me true way how I can add additional view model on a page? Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload