Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is actually an addendum to @Raynos answer, but it's long enough that I need answer-formatting instead of comment-formatting.</p> <ol> <li><p>Clearly OP wants to bind to <code>change</code> and <code>add</code> here, but other people may want to bind to <code>destroy</code> as well. There may be other events (I'm not 100% familiar with all of them yet), so binding to <code>all</code> would cover all your bases.</p> <p><code>remove</code> also works instead of <code>destroy</code>. Note that both <code>remove</code> and <code>destroy</code> fire when a model is deleted--first <code>destroy</code>, then <code>remove</code>. This propagates up to the collection and reverses order: <code>remove</code> first, then <code>destroy</code>. E.g.</p> <pre><code>model event : destroy model event : remove collection event : destroy collection event : remove </code></pre></li> <li><p>There's a gotcha with custom event handlers described in <a href="http://japhr.blogspot.com/2011/11/delegated-events-in-backbonejs-im-idiot.html" rel="nofollow">this blog post</a>. </p> <p><strong>Summary</strong>: Model-level events should propagate up to their collection, but can be prevented if something handles the event first and calls <code>event.stopPropagation</code>. If the event handler returns <code>false</code>, this is a jQuery shortcut for <code>event.stopPropagation(); event.preventDefault();</code></p></li> <li><p>Calling <code>this.bind</code> in a model or collection refers to <strong>Underscore.js's</strong> bind, <strong>NOT</strong> jQuery/Zepto's. What's the difference? The biggest one I noticed is that you cannot specify multiple events in a single string with space-separation. E.g.</p> <pre><code>this.bind('event1 event2 ...', ...) </code></pre> <p>This code looks for the event called <code>event1 event2 ...</code>. In jQuery, this would bind the callback to <code>event1</code>, <code>event2</code>, ... If you want to bind a function to multiple events, bind it to <code>all</code> or call <code>bind</code> once for each event. There is an <a href="https://github.com/documentcloud/backbone/pull/640" rel="nofollow">issue</a> filed on github for this, so this one will hopefully change. For now (11/17/2011), be wary of this.</p></li> </ol>
 

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