让我们假设我有一个big collection
,我想将这个大集合的一个子集用于不同的视图。如何获得已过滤的集合
我试过下面的代码,但它不起作用,因为filtered collection
实际上是一个新的代码,它并没有引用BigCollection instance
。
我的问题是:
我怎样才能得到一个集合,这是BigCollection instance
的子集?
这是我的代码。请参阅评论更多信息:
// bigCollection.js
var BigCollection = Backbone.Collection.extend({
storageName: 'myCollectionStorage',
// some code
});
// firstView.js
var firstView = Marionette.CompositeView.extend({
initialize: function(){
var filtered = bigCollection.where({type: 'todo'});
this.collection = new Backbone.Collection(filtered);
// the issue is about the fact
// this.collection does not refer to bigCollection
// but it is a new one so when I save the data
// it does not save on localStorage.myCollectionStorage
}
});
+1是的,你的想法很好。只有一个问题,当我从firstView创建新模型时,视图不会被重新渲染。是正常的吗? –
@ lorraine-bernard:是的,这很正常。你应该手动渲染视图。 –