When binding event handlers for Backbone events such as add
, remove
, etc. it's a good idea to pass a parameter that will switch context. While sometimes it's not necessary, in most cases it will make your code more consistent and easier to understand. This rule applies to on
and once
operators.
The following patterns are considered warnings:
Backbone.Model.extend({
initialize: function() {
this.on('change', this.modelChanged);
}
});
Backbone.Model.extend({
initialize: function() {
this.on({'change': this.modelChanged});
}
});
The following patterns are not warnings:
Backbone.Model.extend({
initialize: function() {
this.on('change', this.modelChanged, this);
}
});
Backbone.Model.extend({
initialize: function() {
this.on({'change': this.modelChanged}, this);
}
});
If you want context to be changed to be the object that triggered this event, you will need to disable this rule.