I'm struggling with understanding event binding in a Backbone subview. Here's how my view is set up:
TenantView = Backbone.View.extend({
events: {
"click": "setActive"
},
initialize: function() {
this.parentEl = this.options.parentEl;
return this.render();
},
template: new EJS({
url: '/scripts/templates/tenant.ejs'
}),
render: function() {
$(this.parentEl).children('ul').append(this.template.render(this.model));
return this;
},
setActive: function(event) {
return this.model.set('active', true);
}
});
The template just contains an li
with an a
inside. However, I'm having trouble with click events not being captured in my view and the setActive
method not being triggered.
When I add an el
property like el: 'li'
to my view, one of the views works correctly and triggers the setActive
function. But the second view doesn't respond at all. Upon inspecting the el
property during view initialization, the working view's el
points to the correct li
, while the problematic view's el
points to the first li
it finds on the page.
I'm completely lost on the purpose of the el
property.
My question is, how can I properly bind a click event in a view to the setActive
function for that view?
Any insights would be greatly appreciated.
Best regards, Felix