Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your view is only showing a single attribute - for example, if it's a checkbox showing some boolean attribute of your model - you should listen to the 'change:attribute_name' event for that attribute, as described in <a href="http://documentcloud.github.com/backbone/#Model" rel="noreferrer">the Backbone docs</a>.</p> <p>If your view is more complex and relies on multiple model attributes - for example if it's a view for "To Do" list item that has "done", "text", and "dueDate" elements, then listen for the 'change' event. In this case, you can either either choose to update all of the elements on each event, or you can use changedAttributes() to determine which elements need updating.</p> <p>To illustrate ...</p> <h2>Update attributes using 'change:attribute_name' events:</h2> <p>This style works well for simple views where the number of model attributes being rendered is &lt; 3 or so. More than that and the code gets a bit cumbersome.</p> <pre><code>model.bind('change:done', function() { view.doneElement.checked = model.get('done'); }); model.bind('change:text', function() { view.textElement.value = model.get('text'); }); model.bind('change:dueDate', function() { view.dueDateElement.value = model.get('dueDate'); }); </code></pre> <h2>Update everything on 'change' events:</h2> <p>This style works well for complex views that render 4 or more attributes (the 3/4 attribute count is just a rough guideline, based mostly on my personal opinion). </p> <pre><code>model.bind('change', function() { view.doneElement.checked = model.get('done'); view.textElement.value = model.get('text'); view.dueDateElement.value = model.get('dueDate'); }); </code></pre> <p>The downside to this is that for any change, every element of the view is updated. So, for example, if a person marks a todo item as "done", the text will be re-rendered, possibly losing whatever selection they may have had there. Sometimes that sort of thing is an issue, sometimes it isn't - you'll have to decide based on what exactly your view is doing.</p> <h2>Update only what's changed since the last 'change' event:</h2> <p>This is the more nuanced variation of the above, and combines the best of both approaches. It updates the view elements that need updating based on the changedAttributes() results.</p> <pre><code>model.bind('change', function() { var diff = model.changedAttributes(); for (var att in diff) { switch(att) { case 'done': view.doneElement.checked = model.get('done'); break; case 'text': view.textElement.value = model.get('text'); break; case 'dueDate': view.dueDateElement.value = model.get('dueDate'); break; } } }); </code></pre> <p>Finally, I'll note that there's yet another variation on this that involves having the view store a hash of what values it's displaying, and passing that into the changedAttributes() method. That's typically not necessary, so I won't bore you with the details here.</p>
    singulars
    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