[英]backbone.js view events not firing
I am sort of a backbone.js n00b so please bear with me. 我有点主力军,所以请多多包涵。
I am trying to dynamically generate a list of contacts using backbone.js, and attach an onclick event handler to each, to no avail. 我正在尝试使用骨干.js动态生成联系人列表,并将onclick事件处理程序附加到每个列表,但无济于事。 My views are rendering fine, but click event does not register.
我的视图呈现良好,但单击事件未注册。 Here is my code:
这是我的代码:
App.Views.Contact = Backbone.View.extend({
initialize: function() {
this.render();
},
events: {
'click': 'select',
},
select: function(event) {
console.log('contact selected');
},
render: function() {
$('#contacts').append(tmplContact(this.model.toJSON()));
}
});
UPDATE: here is the code for the corresponding collection 更新:这是相应集合的代码
App.Collections.Contact = Backbone.Collection.extend({
initialize: function() {
this.bind('reset', function() {
console.log('COLLECTION RESET');
});
},
model: App.Models.Contact,
comparator: function(model) {
return model.get('name');
}
});
UPDATE #2: collection initialization and population 更新2:集合的初始化和填充
App.Collections.roster = new App.Collections.Contact;
function getContacts() {
socketRequest('friends', function(data) {App.Collections.roster.reset(data)});
}
UPDATE #3: model definition 更新3:模型定义
App.Models.Contact = Backbone.Model.extend({
initialize: function() {
this.set({id: this.get('token')});
this.set({avatar: parseAvatar(this.get('avatar'))});
this.set({name: parseName(this.toJSON())});
this.set({view: new App.Views.Contact({model: this})});
},
defaults: {
username: ''
}
});
You need to use the View.el property in order to get the events working. 您需要使用View.el属性才能使事件正常运行。
App.Collections.Contact = Backbone.Collection.extend({
initialize: function() {
this.bind('reset', function() {
console.log('COLLECTION RESET');
});
},
model: App.Models.Contact,
comparator: function(model) {
return model.get('name');
},
render: function() {
;
}
});
App.Views.Contacts = Backbone.View.extend({
el: '#contacts',
initialize: function() {
this.collection = new App.Collection.Contact;
this.collection.bind('reset', this.render, this);
var that = this;
socketRequest('friends', function(data) {
this.reset(data);
});
},
render: function() {
$(this.el).html('put your template here');
this.collection.each(this.addOne, this);
return this;
},
addOne: function(contact) {
var view = new App.Views.Contact({model: contact});
contact.view = view; // in case you need the ref on the model itself
$(this.el).append(view.render().el);
}
});
App.Views.Contact = Backbone.View.extend({
el: '',
tagName: 'div',
events: {
'click': 'select',
},
select: function(event) {
console.log('contact selected');
},
render: function() {
$(this.el).html(htmltmplContact(this.model.toJSON()));
return this;
}
});
a small improvement to your model code: 对您的模型代码有一点改进:
(notice that i removed the view creation, this is important!) (注意,我删除了视图创建,这很重要!)
App.Models.Contact = Backbone.Model.extend({
parse: function(c) {
c.id = c.token;
c.avatar = parseAvatar(c.avatar);
c.name = parseName(c);
return c;
},
defaults: {
username: ''
}
});
and to kick things off: 并开始:
App.Views.roster = App.Views.Contacts;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.