【发布时间】:2013-12-19 06:36:57
【问题描述】:
.on、.listenTo、.bind 有什么区别?
我在这里对它们进行了测试,它们似乎在做同样的事情:回调。
var NewStatusView = Backbone.View.extend({
events: {
"submit form": "addStatus"
},
initialize: function(options) {
// using .on
//this.collection.on("add", this.clearInput, this);
// or using bind:
//_.bindAll(this, 'addStatus', 'clearInput');
//this.collection.bind('add', this.clearInput);
// or using listenTo:
_.bindAll(this, 'addStatus', 'clearInput');
this.listenTo(this.collection, 'add', this.clearInput) ;
},
addStatus: function(e) {
e.preventDefault();
this.collection.create({ text: this.$('textarea').val() });
},
clearInput: function() {
this.$('textarea').val('');
}
});
什么时候和在什么场景下使用哪个是最好的?
【问题讨论】:
-
你访问过github上的链接页面吗?
-
Events.bind = Events.on;Events.unbind = Events.off;是否意味着bind与on相同? -
是的,两种方法都做同样的事情。检查第一行backbonejs.org/#Events-on
-
现在我明白了。谢谢你! :)
标签: javascript backbone.js callback