I have a view that contains nested views of the same type. Because of this, my delegateEvents selector needs to be careful to only select the top-level elements and not the elements in the child views.
The following code, used within the context of my view, successfully selects the element I wish to bind:
var $link = this.$('> .node > .indent > a'); // success!
The delegateEvents object, using the same selector, does not hook up the event at all:
events: {
'click > .node > .indent > a': 'toggleGrouped' // fail :(
}
Note that I have confirmed that the event hookup does work with other, simpler selectors, so it’s not an issue with rendering.
What am I doing wrong?
It probably has to do with jQuery’s
delegateevent not liking the> .node > .indent > aselector.You can confirm this by adding the following line of code in your view’s render method. (this is what Backbone is doing in
delegateEvents)If it still doesn’t work then the issue has to do with the
delegateevent and not backbone.A workaround is to bind to the click event after all the rendering is done in the render method.
You will also have to bind the context of
toggleGroupedin the initialize method since it’s no longer being automatically bound.