【发布时间】:2012-07-29 01:11:39
【问题描述】:
我正在阅读有关如何使用 Backbone.js 的信息,但与最新版本的 Backbone 库相比,似乎所有在线可用的文章都已过时?这没有帮助!
例如,我看到 nettuts 的一篇文章(首先出现在 Google 搜索结果中)讨论了有关使用 Backbone.Controller 的内容,但该文章已从 Backbone 中删除,因此难以发现。
但无论如何,我的问题是关于 Backbone 中的事件对象。
使用“视图”,您可以做到...
var ContactsView = Backbone.View.extend({
initialize: function(){
console.log('View initialized');
},
events: {
'change select': 'displaySelected'
},
displaySelected: function (event) {
console.log('get model data and display selected user', event);
}
});
...但是您不能将events 属性与Collection 一起使用,而是需要在initialize 方法中使用jQuery 的bind 方法...
var Contacts = Backbone.Collection.extend({
model: Contact,
initialize: function(){
this.bind('add', this.model_added, this);
},
model_added: function(){
console.log('A new model has been created');
}
});
...首先,这是为什么呢?
但更重要的是,第三个参数this 是为了什么?我在 jQuery 文档上查找了 bind(因为在 Backbone 网站上没有提到 bind)并且它没有第三个参数?
【问题讨论】:
标签: backbone.js