我有一个已经装满模型的集合,我需要用一个模型更新这个集合并在页面上显示它(模型)。因此,按照文档,我从视图中的服务器获取模型:
this.collection.fetch(
{
add: true,
data: FILTER + '&page=' + (CURRENT_PAGE + 1),
success: function(response){}
});
但是这个新模型并没有出现在页面上,但是集合得到了新模型。我想必须触发集合的某些方法,但事实并非如此。
提前抱歉,Backbone的新手)
风景:
var EcoNatureCardInListView = Backbone.View.extend({
tagName: 'tr',
className: 'nature-card-tr',
template: $('#natureCardsListTR').html(),
render: function(){
var tmpl = Handlebars.compile(this.template);
this.$el.html(tmpl(this.model.toJSON()));
return this;
}
});
var EcoNatureCardsListView = Backbone.View.extend({
el: $('#nature-cards-wrapper'),
events: {
"click a.short-eco-entity": "showFullEcoNatureCard",
"click #add-cards": "addCardsOnPage"
},
initialize: function(){
$("#nature-cards-list").html("");
this.collection = new EcoNatureCardCollection();
this.collection.fetch({
success: function(response){}
});
this.collection.on('reset', this.render, this);
this.collection.on('add', this.add, this);
},
render: function(){
var that = this;
_.each(this.collection.models, function(item){
that.renderEcoNatureCard(item);
}, this);
$(addCards).show();
$("#total-objects").text(TOTAL_OBJECTS);
$("#filtered-objects").text(FILTERED_OBJECTS);
if (this.collection.length < 20){
$(addCards).hide();
}
},
renderEcoNatureCard: function(item){
var ecoNatureCardInListView = new EcoNatureCardInListView({
model: item
});
$('#nature-cards-list').append(ecoNatureCardInListView.render().el);
},
showFullEcoNatureCard: function(e){
var _id = $(e.currentTarget).attr('value');
var natureCard = ecoNatureCardsListView.collection.where({ _id: _id })[0];
if (typeof(fullEcoNatureCardView) === 'undefined'){
fullEcoNatureCardView = new FullEcoNatureCardView(natureCard);
} else {
fullEcoNatureCardView.initialize(natureCard);
}
},
addCardsOnPage: function(){
this.collection.fetch({
add: true,
data: FILTER + '&page=' + (CURRENT_PAGE + 1),
success: function(response){}
});
},
filterDocs: function(FILTER){
$("#nature-cards-list").html("");
//$(loading).show();
this.collection.fetch({
data: FILTER,
success: function(response){}
});
}
});
PS版本0.9.2