Reputation: 578
So I have a Backbone view in which I declare it's className. I'm trying to bind a click event to that class. So something like this:
className: "question"
events:
"click .question": -> console.log("clicked")
This doesn't seem to work. It seems to be because the element isn't inside the view itself. So if I create an element within a template, I can bind to that just fine. I should be able to bind to the view itself right? Any help is appreciated. Thanks!
Upvotes: 3
Views: 1552
Reputation: 434665
From the fine manual:
delegateEvents
delegateEvents([events])
[...] Events are written in the format
{"event selector": "callback"}
. Thecallback
may be either the name of a method on the view, or a direct function body. Omitting theselector
causes the event to be bound to the view's root element (this.el
).
So you want your events
to look like this:
events:
'click': -> console.log('clicked')
Demo: http://jsfiddle.net/6W6QE/
Upvotes: 7