Setting this.el
or this.$el
can cause issues, since Backbone will not automatically bind events to the element. Use setElement
function instead which will cache a reference to an element and bind all events.
The following patterns are considered warnings:
Backbone.View.extend({
initialize: function() {
this.$el = $(".test");
}
});
Backbone.View.extend({
initialize: function() {
this.el = $(".test")[0];
}
});
The following patterns are not warnings:
Backbone.View.extend({
initialize: function() {
this.setElement($(".test"));
}
});
If you always call delegateEvents
manually, you can still set this.el directly.