我有我的模型
var Client = Backbone.Model.extend({});
var Colony = Backbone.Collection.extend({
url: '/presence/knock',
model: Client
});
意见
var ClientView = Backbone.View.extend({
initialize: function(){
this.render();
},
template: _.template('...'),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var ColonyView = Backbone.View.extend({
initialize: function(){
_.bindAll(this, 'addOne', 'addAll');
this.collection.bind('add', this.addOne);
this.collection.bind('refresh', this.addAll);
this.collection.bind('change', this.addAll);
},
addOne: function(item){
console.log('addOne', item);
var view = new ClientView({
model: item
});
$(this.el).append(view.render().el);
},
addAll: function(){
var self = this;
this.collection.each(function(item){
self.addOne(item);
});
},
render: function(){
this.addAll();
return this;
}
});
我用彗星更新收藏/presence/knock
。
var colony = new Colony([{
ip: '127.0.0.1',
name: 'localhost.localdomain',
lsup: 'now'
}]);
setInterval(function(){
colony.fetch({
update: true
});
}, 5*1000);
var colonyView = new ColonyView({
collection: colony
});
$("#clients-board").append(colonyView.render().el);
第一次渲染colonyview 时addOne
获取客户端作为参数,因为它是通过addAll 调用的。但是下次addOne
通过add
事件调用时,我在检查员item
中看到的不是client
r {cid:“c606”,属性:对象,集合:r,_changed:false,_previousAttributes:对象...}