Tôi có một câu hỏi, những thứ thực sự cơ bản mà tôi nghĩ nhưng:Xương sống: Nhiều lượt xem đăng ký sự kiện của một bộ sưu tập? Đây có phải là một thực hành không tốt?
Tôi chỉ thấy các ví dụ với chế độ xem bộ sưu tập và chế độ xem đơn tùy thuộc vào bộ sưu tập đang được cập nhật. Điều gì xảy ra nếu bạn có nhiều chế độ xem đang cố gắng đăng ký một sự kiện thu thập, tức là đặt lại, addOne, addAll, v.v ...
Tôi có thiếu điểm nào về việc làm/không thực hiện việc này không? Bạn có ví dụ nào về điều này không? Điều đó thậm chí có ý nghĩa không?
Bất kỳ thông tin được nhiều đánh giá
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});
Cảm ơn cho câu trả lời, làm cho tinh thần! –