Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Under the hood, Backbone uses jQuery's "delegate" to wire up the events. So it's not that this is "like" jQuery, it is jQuery doing it for us.</p> <p>To prevent scoping problems and ensure that our View events are only working with the HTML/DOM elements that we want, all <code>events</code> declarations are scoped to the View's <code>el</code>.</p> <p>When a Backbone view is instantiated, an <code>el</code> attribute is either generated for you, or assigned by you. This gives you the opportunity to either attach your view to an existing chunk of the DOM or to create a new chunk of HTML that can be attached to the DOM.</p> <p>Here's how you can attach to the existing DOM:</p> <p><pre><code>MyView = Backbone.View.extend({ // ... });</p> <p>var existingDomEl = $("#someElement"); new MyView({ el: existingDomEl }); </pre></code></p> <p>By specifying the <code>el</code> when we instantiate the view, or by specifying it directly in our view definition, we use an existing element.</p> <p>If we omit the <code>el</code> from the constructor options and from the view definition, Backbone will generate an <code>el</code> for us. By default it will generate a div in <code>el</code> when the view is instantiated.</p> <p>Once a View has it's <code>el</code>, whether through generation or assignment, the view's event declarations are delegated via jQuery, scoped to the view's <code>el</code>.</p> <p>let's say you have the following html:</p> <pre><code>&lt;div id="foo"&gt; &lt;button class="btnSay"&gt;Say!&lt;/button&gt; &lt;/div&gt; &lt;div id="bare"&gt; &lt;button class="btnSay"&gt;Say, Part 2!&lt;/button&gt; &lt;/div&gt; </code></pre> <p>with your example View, we could assign a view to either the <code>foo</code> or the <code>bar</code> element, and only the button within that element would get the click event.</p> <pre><code> var PersonView = Backbone.View.extend({ .... events : { "click button.btnSay" : "saySomething" }, saySomething : function(){ .... } ... }); new PersonView({ el: $("#foo") }); </code></pre> <p>Now when you click the button in the <code>foo</code> div, you'll get the <code>saySomething</code> callback fired. But because the events for PersonView were scoped within the <code>el</code> for that instance of PersonView, clicking the button within <code>bar</code> will never fire the callback.</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. 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