I'm having problems understanding the event binding in a Backbone subview. My view is defined as follows:
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 simply consists of an li
containg an a
. Done this way, clicks events on my view are not catched, my method setActive
is never triggered.
When I extend my view by an el
property like el: 'li'
one of my (2) views acts properly and triggers the setActive
function. The second view does not react at all. If I insepct the el
property during the views initialization, the working view's el
property points to the right li
, the failing views el
points to the first li
that can be found in the page.
As one can see, I am totally lost when it comes to the meaning of the el
property.
Question is, how can I bind a click on view to this very views setActive
function?
Can anyone enlighten me please?
Regards Felix