请我有一个挑战,推进这个项目(没有服务器端)。考虑这个例子:
var date = Date.now();
var item1 = new Item({date: date, title: 'breakfast', value: 100, history: false});
var item2 = new Item({date: date, title: 'lunch', value: 200, history: false});
var item3 = new Item({date: date, title: 'dinner', value: 300, history: false});
var items = new Backbone.Collection;
items.add(item1, item2, item3);
//Create a new model derived from these models and save in collection
var sum = 0;
items.each(function(elem) {
sum += parseInt(elem.get('value'));
//elem.destroy();
}
var report = new Item({date: date, title: 'Report', value: sum, history: true});
items.add(report);
运行正常。将显示4个模型。但我不希望在计算报表后将这3个模型保留在集合中。这3个模型应被销毁。刷新页面时,应仅显示报表模型。如何实现此目的?
1条答案
按热度按时间u1ehiz5o1#
请使用reset函数,而不要加入报表模型:
items.reset(report)