Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Funny you should ask... I just solved a similar problem in an app I'm working on, and lo and behold, this was the top question under the Backbone.js tag. </p> <p>Not sure that we were having the same issue, but my problem was that the button I was trying to attach to was actually outside of the scope of my view. Backbone views have an <code>el</code> property, and <code>click</code> events are only attached to children of that root element.</p> <p>In my app, I had a list view which had a <code>ul</code> as it's root element. Both my button and the list were siblings inside of a <code>div</code> tag:</p> <pre><code>&lt;div&gt; &lt;ul id="my-list-view"&gt;&lt;/ul&gt; &lt;button id="save"&gt;Save&lt;/button&gt; &lt;/div&gt; </code></pre> <p>My code worked initially because I set up the click handler using a raw jQuery selector in the <code>initialize</code> function. I was actually reaching outside of the scope of my view and attaching the click handler, which wasn't at all my intention. When I refactored to try to make it fit the Backbone convention, it failed to fire.</p> <p>Try using a regular jQuery selector in your code, and if it works, you probably have a similar scoping problem. Like this:</p> <pre><code>initialize: function() { $('#locationAdd').click(addClickEvent); this.render(); } </code></pre> <p>That's not the Backbone way of doing this--you want to use the <code>events</code> hash like you have done in your code. But you may need to nest your collection view inside of a control-level view.</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