Note that there are some explanatory texts on larger screens.

plurals
  1. POknockback - backbone model reset, knockout if render
    primarykey
    data
    text
    <p>I am using backbone for persistence and knockout for binding, i.e. I am using knockback On page load, all is well. When I click on the check box, based on the value of IsChecked, either yes or no is displayed.</p> <p>When I click on reset model button, the previously seen behavior is lost. That is, if 'yes' is being displayed and I click on reset, then 'no' will never be displayed no matter the check box state.</p> <p>If I use visible binding it will work but I need to get this 'if' to work so that I can decide whether to render some form elements depending on user criteria.</p> <p>Any help is appreciated. Thanks.</p> <p>Here is my sample code:</p> <pre><code>&lt;script type="text/javascript"&gt; var model_a; var collection_ac; var example_new_model_a; $(document).ready(function () { model_a = Backbone.Model.extend({ idAttribute: 'A_Pid', defaults: { A_Pid: null, ChildName: "", IsChecked: false, Show: false, }, }); example_new_model_a = new model_a(); example_new_model_a.set('ChildName', 'child of test'); vm = kb.viewModel(example_new_model_a); ko.applyBindings(vm, $('#div1')[0]); }); var vm; function reset_model() { kb.utils.release(vm); vm = null; example_new_model_a = new model_a(); example_new_model_a.set('ChildName', 'child of test2'); vm = kb.viewModel(example_new_model_a); ko.applyBindings(vm, $('#div1')[0]); } function show_div() { example_new_model_a.set('Show', true); } </code></pre> <p></p> <pre><code>&lt;div id="div1"&gt; &lt;div data-bind="visible: Show"&gt; Check or Uncheck &lt;input type="checkbox" data-bind="checked: IsChecked" /&gt; &lt;!-- ko if:IsChecked --&gt; &lt;p&gt;yes&lt;/p&gt; &lt;!-- /ko --&gt; &lt;!-- ko if:!IsChecked() --&gt; &lt;p&gt;no&lt;/p&gt; &lt;!-- /ko --&gt; &lt;button data-bind="click: reset_model"&gt;Reset&lt;/button&gt; &lt;/div&gt; &lt;button data-bind="click: show_div"&gt;Show&lt;/button&gt; </code></pre> <p></p> <p><strong>Edit</strong></p> <p>I found that if I add the following code, even after pressing reset, it works. After reading <a href="https://stackoverflow.com/questions/8537397/knockout-js-how-to-force-view-refresh-instead-of-using-ko-observable">How to force a view refresh without having it trigger automatically from an observable?</a> and <a href="https://stackoverflow.com/questions/7516636/knockout-js-templates-foreach-force-complete-re-render">Knockout.js Templates Foreach - force complete re-render</a>, I tried the template with if and it works. Not sure how to call value has mutated from knockback view model so unless there is a better way, I will use this workaround.</p> <pre><code>&lt;div data-bind="template: { name: 'test-template-1', if:IsChecked}"&gt;&lt;/div&gt; &lt;script type="text/html" id="test-template-1"&gt; &lt;p&gt;yes2&lt;/p&gt; &lt;/script&gt; &lt;div data-bind="template: { name: 'test-template-2', if:!IsChecked()}"&gt;&lt;/div&gt; &lt;script type="text/html" id="test-template-2"&gt; &lt;p&gt;no2&lt;/p&gt; &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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