【发布时间】:2014-03-04 02:27:00
【问题描述】:
我正在尝试登录,然后获取项目列表。
从登录模型它尝试reset 项目列表。问题是当集合被重置时,集合 UI 没有捕捉到它。
Login Model的登录功能
login: function() {
this.save(
{}, {
success: function(resp) {
var list = [];
list[0] = resp.get("0");
list[1] = resp.get("1");
dashboardList.reset(list);
}
});
}
仪表板列表
var DashboardList = Backbone.Collection.extend({
model: DashboardModel
});
var dashboardList= new DashboardList();
仪表板列表视图
var DashboardListView = Backbone.View.extend({
initialize: function() {
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.addAll, this);
},
addOne: function(item) {
var viewItem = new DashboardView({model: dashboardModel});
this.$el.append(viewItem.render().el);
},
addAll: function() {
this.collection.forEach(this.addOne, this);
},
render: function() {
this.addAll();
}
});
仪表板项目视图
var DashboardView = Backbone.View.extend({
template:_.template('<div>'+
'<h3><%= campaignName %></h3>'+
'<span><%= orderedAndGoal %>, </span>'+
'<span><%= status %>, </span>'+
'<span><%= endDate %>, </span>'+
'</div>'),
initialize: function() {
this.model.on('change', this.render, this);
},
render: function() {
var attributes = this.model.toJSON();
this.$el.html(this.template(attributes));
this.$el.appendTo('.container');
}
});
【问题讨论】: