Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Backbone events are executed synchronously. That means that unless you (or some library) has overridden some part of the event handling, the change handlers will have processed as soon as you execute the next line of code.</p> <p>In you code example there is another problem. When you call</p> <pre><code>user.get('settingsJSON').apps = appsEnabled; </code></pre> <p>The <code>change</code> event will <strong>not</strong> fire, because the value of <code>settingsJSON</code> has not been changed, merely the contents of the object were modified. The <code>model.attributes.settingsJSON</code> is still the same object as before.</p> <p>The events are fired only when you call <code>set</code> on the property, and the new value is a <strong>different</strong> object. For example:</p> <pre><code>user.set('settingsJSON', _.extend({}, user.get('settingsJSON'), {apps:appsEnabled}); </code></pre> <p>Another problem, it would seem, is that your event handlers, if triggered, would cause the <code>change</code> event being fired twice for the property which was first set: </p> <pre><code>this.on("change:settingsJSON", function(model){ //-&gt; changes settings, and set triggers change model.set({settings: JSON.stringify(model.get('settingsJSON'))}); }); this.on("change:settings", function(model){ //-&gt; changes settingsJSON, and set triggers change model.set({settingsJSON: JSON.parse(model.get('settings'))}); }); </code></pre> <p>To solve that issue, call <code>set</code> with <code>{silent:true}</code> or modify the <code>model.attributes</code> hash directly.</p> <p><strong>Edited</strong> with corrections by @muistooshort.</p> <p><strong>Edited</strong> again with further corrections</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