我有一个问题,我认为非常基本的问题但是:
我只见过 Collection View 和依赖于正在更新的集合的单一 View 的示例。如果您有多个 View 试图订阅一个集合事件,即重置、addOne、addAll 等...
关于做/不做这件事,我是否遗漏了一些要点?你有这方面的例子吗?这有意义吗?
非常感谢任何信息
var Coll = Backbone.Collection.extend({
model: SingleModel,
url: 'service',
initialize: function(){
console.log('collection inited')
}
});
var SingleModel = Backbone.Collection.extend({});
var CollView = Backbone.View.extend({
el: 'ul',
template: Handlebars.compile(someContainerTemplate),
init: function(){
_.bindAll(this, 'render', 'addAll', 'addOne');
this.collection.bind("reset", this.addAll);
this.collection.fetch();
},
render: function(){
$(this.el).append(this.template())
},
addAll: function(){
this.collection.each(this.addOne);
},
addOne: function(model){
var view = new SingleView({ model: model })
}
})
var SingleView = Backbone.View.extend({
tagName: "li",
events: {
"click .delete": "remove"
},
template: Handlebars.compile(someTemplateForSingleItem),
initialize: function() {
_.bindAll(this,'render');
this.model.bind('save', this.addOne);
this.model.bind('destroy', removeEl);
},
remove: function(){
this.model.destroy();
},
removeEl: function(){
$(this.el).remove();
},
render: function() {
var context = this.model.toJSON();
return $(this.el).append(this.template(context));
},
})
// standard so far (excluding any bad practices),
// but what if you have another view dependent on
// say the number of length of the collection, and
// you want it to update if any single models are destroyed
var HeaderView = Backbone.View.extend({
tagName: "div#header",
template: Handlebars.compile(someHeaderTemplate),
initialize: function() {
_.bindAll(this,'render');
this.model.bind('save', this.addOne);
},
render: function() {
//assigning this collection length
var context = this.collection.length;
return $(this.el).append(this.template(context));
},
});
var coll = new Coll();
new CollView({ collection: coll });
new HeaderView({ collection: coll});
最佳答案
您所做的非常好,这也是使用 Backbone 的部分原因。来自Backbone introduction :
Whenever a UI action causes an attribute of a model to change, the model triggers a "change" event; all the Views that display the model's state can be notified of the change,
请注意,他们说的是“所有 View ”,而不是“ View ”。
单个集合的多个 View 的一个示例是聊天系统。假设您有一组在线用户;那么您可能在标题中有一个简单的 View ,显示在线人数,另一个 View (属于同一集合)列出用户:
var User = Backbone.Model.extend({});
var OnlineUsers = Backbone.Collection.extend({
model: User
});
var CounterView = Backbone.View.extend({
tagName: 'span',
initialize: function() {
_.bindAll(this, 'render');
this.collection.on('add', this.render);
// other interesting events...
},
render: function() {
this.$el.text(this.collection.size());
return this;
}
});
var ListView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.collection.on('add', this.render);
// other interesting events...
},
render: function() {
var html = this.collection.map(function(m) {
return '<p>' + m.get('name') + '</p>';
});
this.$el.html(html.join(''));
return this;
}
});
那么您将拥有一个 OnlineUsers
实例,但您的 CounterView
和 ListView
实例都将监视它。当人们上线或下线时,两种 View 都会根据需要进行更新。
简单演示:http://jsfiddle.net/ambiguous/eX7gZ/
上面的示例情况听起来正是您正在做的事情,而这正是 Backbone 所要做的事情。干得好。
关于design-patterns - 主干 : Multiple views subscribing to a single collection's event? 这是一种不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341141/