In one of my views I have:
events: {
'click .tab': 'doSomething',
},
then:
doSomething: function(){
...
},
This is a recurring structure in my views, but for some reason the 'doSomething' function is not being triggered by the click in this view.
When are the elements being bound to the event?
Any tips on debugging this?
In this article, I will show you how to bind DOM events with the View method in backbone.js. At the time of writing, to use backbone.js we need to add three dependencies, the first is the "jQuery" library, the second is the "Underscore.js" library and the last is the Backbone.js library. This associates the click event to the button on the DOM.
At the time of writing, to use backbone.js we need to add three dependencies, the first is the "jQuery" library, the second is the "Underscore.js" library and the last is the Backbone.js library. This associates the click event to the button on the DOM.
If element is bound to event, it reacts not only on action originated at itself, but also on all acts bubbled from children DOM elements. Selector parameter eliminates possible origin of event for children that satisfied it. Look at the DOM tree below:
Often it is required, or just case of convenience, to bind event to DOM elements that do not yet exist. Often, such element is generated by third party code and there is no easy way to predict when the elements are actually added to the page.
link to fiddle here : http://jsfiddle.net/7xRak/
Omitting the selector causes the event to be bound to the view's root element (this.el
).
if your class="tab"
is views DOM element means this.el
then you should bind event as
events : {
'click' : 'dosomething'
}
and for inner element in this.el
like
<div class="tab">
<span class="inner"></span>
</div>
then you should bind event as,
events : {
'click' : 'dosomething'
'click .inner' : 'onInnerClick'
}
document : http://backbonejs.org/#View-delegateEvents
The delegation happens during the delegateEvents
method at the end of the view constructor.
http://documentcloud.github.com/backbone/docs/backbone.html#section-144
Try manually calling this.delegateEvents()
inside render
before returning this
. Are you futzing with this.el
inside your initialize
or render
methods in a way that backbone isn't expecting?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With