JavaScript
1
Вклад в тег
var CommentsListViewClass = Backbone.View.extend({
initialize: function(options){
this.templateData = options && options.templateData || {};
this.listenTo(this.collection, "add", this.renderItem);
this.listenTo(this.collection, "add remove sync", this.renderUpdate);
this.listenTo(this.collection, "sync", this.resortItems);
this.itemViews = [];
},
render: function(){
this.$el.empty();
},
renderUpdate: function(){
$('.comments_count').html(this.collection.length);
},
resortItems: function(){
this.collection.each(_.bind(function(m,i){
var viewEl = this.$("."+m.cid);
this.placeItem(viewEl, m);
}, this));
},
placeItem: function(itemEl, model){
var index = _.indexOf(this.collection.models, model);
//console.log(index, itemEl,model);
if(index == 0){
console.log("#"+ index ,model.id, 'ontop');
this.$el.prepend(itemEl);
} else {
var pIndex = index-1;
var cid = this.collection.models[pIndex].cid;
this.$el.find('.'+cid).after(itemEl);
console.log("#"+ index, model.id, "after " + this.collection.models[pIndex].id );
}
},
renderItem: function(model, collection, options){
var item = new CommentsItemViewClass({
model: model,
templateData: this.templateData
});
var itemEl = item.render();
this.placeItem(itemEl, model);
}
});
var item = new CommentsItemViewClass({
model: model,
templateData: this.templateData
});
//получим DOM-элемент вида
var itemEl = item.render();
var index = _.indexOf(this.collection.models, model);
//console.log(index, itemEl,model);
if(index == 0){
this.$el.prepend(itemEl);
} else {
var pIndex = index-1;
//cid предыдущей модели
var cid = this.collection.models[pIndex].cid;
this.$el.find('.'+cid).after(itemEl);
}
this.collection.each(_.bind(function(m,i){
var viewEl = this.$("."+m.cid);
this.placeItem(viewEl, m);
}, this));