NotSoMuchNo
NotSoMuchNo

Reputation: 17

Creating a backbone collection from a filtered collection

From reading the documentation, it looks as if passing an array of models into a collection initializer should populate the collections models attribute with the supplied array. However, when I try, I end up with an empty collection. No errors, no indication of why it fails, it simply doesn't do anything? I've verified that my original fetch returns data and that my filter works correctly and returns an array - from there, everything goes south.

I'm trying to do something similar to the following example:

var files = new Backbone.Collection();
files.fetch({
    success: function(collection){
        var filtered = collection.where({ type: 'Software' });
        var filteredCollection = new Backbone.Collection({ models: filtered });
    }
})

When I run this, I get a filteredCollection with an empty models array. Someone mind pointing out the bonehead mistake I'm making? TIA!

Upvotes: 0

Views: 48

Answers (1)

Nazar
Nazar

Reputation: 1799

To create the instance of Backbone Collection you should pass just an array as single argument.

var files = new Backbone.Collection();
files.fetch({
    success: function(collection){
        var filtered = collection.where({ type: 'Software' });
        var filteredCollection = new Backbone.Collection(filtered);
    }
});

Upvotes: 1

Related Questions