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);
this.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 compatibility.
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.org/#Events-on
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With