SHT
SHT

Reputation: 720

BackboneJS + Lodash _.union method

In my Backbone App i'm trying to merge collections by using the _.union-method from Lodash (Underscore).

So I have the following:

var myCollection = _.union([carsCollection], [motorcycleCollection], [bikeCollection]);

when I do console.log(collection) it gives me [child, child, child] where each child contains an array of the Models from the collection and its attributes. So far so good, my question is now:

How can I display this in a View? I tried:

this.insertView(new View({collection: myCollection }));

but that didnt work...

Does anyone know whta the issue is here?

Upvotes: 0

Views: 240

Answers (1)

nikoshr
nikoshr

Reputation: 33344

Backbone collections are not arrays of models, using _.union on them won't produce a collection of models. You have to work with the collection.models and then build a new collection :

var models = _.union(
    carsCollection.models,
    motorcycleCollection.models,
    bikeCollection.models
);
var unitedCollection = new Backbone.Collection(models);

See http://jsfiddle.net/nikoshr/uc5cn/ for a demo

Upvotes: 1

Related Questions