What is the difference between bind() and on() methods in Backbone.js
Documentation for on() : On method documentation at backbone.js
Documentation for bind() : Bind method documentation at underscore.js
Which of the two should be used to bind custom events for objects ?
Usage example:
this.bind('myEvent', this.render, this);
this.on('myEvent', this.render, this);
What is the difference between bind() and on() methods in Backbone.js
Documentation for on() : On method documentation at backbone.js
Documentation for bind() : Bind method documentation at underscore.js
Which of the two should be used to bind custom events for objects ?
Usage example:
this.bind('myEvent', this.render, this);
this.on('myEvent', this.render, this);
Share
Improve this question
asked Apr 7, 2013 at 17:00
Sachin JainSachin Jain
21.8k34 gold badges110 silver badges176 bronze badges
2 Answers
Reset to default 15this.bind('myEvent', this.render, this);
this.on('myEvent', this.render, this);
These are exactly equivalent and are not related to the underscore bind
function.
Here is some code from Backbone source:
// Aliases for backwards patibility.
Events.bind = Events.on;
Events.unbind = Events.off;
So, in both lines of your code, you are calling the same function.
_.bind in underscore has nothing to do with bind in Backbone event object. ( or underscore )
_.bind in underscore is used when you want to bind a function to a context ( wrap it in a closure ). So when the function is called the context (this ) doesnt change .
the Backbone documentation explicitly says the bind method in the event is an alias to the on method. So why do you link to the bind definition in underscore ?
http://backbonejs/#Events-on