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 に答える