最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between bind and on in backbone - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 15
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 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

发布评论

评论列表(0)

  1. 暂无评论