1

假设我有一个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
    }
});
4

2 回答 2

1

用于BigCollection制作过滤集合,如下所示:

    // firstView.js
    var firstView = Marionette.CompositeView.extend({
        initialize: function(){
            var filtered = bigCollection.where({type: 'todo'});
            this.collection = new BigCollection(filtered);
            // now, it will save on localStorage.myCollectionStorage
        }
    });
于 2012-09-29T12:56:42.950 回答
0

您可以将原始模型保存在集合中的变量中,以便在取消应用过滤器后恢复它们,如下所示:

// bigCollection.js
var BigCollection = Backbone.Collection.extend({
    storageName: 'myCollectionStorage',
    // some code
});

// firstView.js
var firstView = Marionette.CompositeView.extend({
    initialize: function(){
        bigCollection.original_models = bigCollection.models;
        bigCollection.models = bigCollection.where({type: 'todo'});
    }
});

然后您可以在切换过滤器时恢复它们:

bigCollection.models = bigCollection.original_models;
于 2012-09-29T12:13:50.107 回答