Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i don't think you're doing anything wrong. the problem is the "if" binding to the root.isEmployee is being applied before the binding to the "with" is being updated. so the code is seeing the update to the isEmployee and then re-evaluating the view from there down, but the current context is still the old person (as that subscription hasn't fired).</p> <p>this is proven via a custom binding in <a href="http://jsfiddle.net/drdamour/X6pC9/2/" rel="nofollow">http://jsfiddle.net/drdamour/X6pC9/2/</a> notice the update receives 2 events, once with the old value cause the isEmployee was updated, and second time with the updated new value. this second update comes from the "with" binding subscription being triggered. The subscription of the 'with' binding happens during the applyBindings call, which happens AFTER your model does a subscription.</p> <p>you can use $data.PropertyName trick to deal with undefined not causing issues. Ala: <a href="http://jsfiddle.net/drdamour/X6pC9/1/" rel="nofollow">http://jsfiddle.net/drdamour/X6pC9/1/</a></p> <pre><code>&lt;span data-bind="with: person"&gt; &lt;span data-bind="text: firstName"&gt;&lt;/span&gt; &lt;!-- ko if: $root.isEmployee --&gt; &lt;span data-bind="text: $data.employeeId"&gt;&lt;/span&gt; &lt;span data-bind="text: $data.employer"&gt;&lt;/span&gt; &lt;!-- /ko --&gt; &lt;/span&gt; </code></pre> <p>the RIGHT way to solve this is to have a PersonVM that has the isEmployee computed, that way you don't bind to the root. see: <a href="http://jsfiddle.net/drdamour/eVXTF/1/" rel="nofollow">http://jsfiddle.net/drdamour/eVXTF/1/</a></p> <pre><code>&lt;span data-bind="with: person"&gt; &lt;span data-bind="text: firstName"&gt;&lt;/span&gt; &lt;!-- ko if: isEmployee --&gt; &lt;span data-bind="text: $data.employeeId"&gt;&lt;/span&gt; &lt;span data-bind="text: $data.employer"&gt;&lt;/span&gt; &lt;!-- /ko --&gt; &lt;/span&gt; </code></pre> <p>and</p> <pre><code>var ViewModel = function(){ var self = this; self.person = ko.observable(); }; var PersonVM = function() { var self = this; this.firstName = ko.observable(); this.employeeId = ko.observable(); this.employer = ko.observable(); self.isEmployee = ko.computed(function(){return self.employer() != null}); } var vm = new ViewModel(); var customer = new PersonVM(); customer.firstName("John"); var employee = new PersonVM(); employee.firstName("Bill"); employee.employeeId(123); employee.employer("ACME"); vm.person(customer); ko.applyBindings(vm); setTimeout(function(){ vm.person(employee); }, 3000); </code></pre> <p>computed is preferred to subscribe methods as it deals with the subscription chain for you and abstracts you away from having to manage all that.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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