2

Im working with a collection and models:

var StuffCollection = Backbone.Collection;
var StuffModel = Backbone.RelationalModel;

in one place I make an instance of that collection with models:

var stuffCollection = new StuffCollection();
// do stuff here to load a bunch of models

in another place I want to clone that collection to edit without editing the original:

var tempStuffCollection = new StuffCollection();
tempStuffCollection.reset(stuffCollection.models);
// do stuff here to edit the collection

but when I edit the models in tempStuffCollection they edit in stuffCollection so instead I tried this:

var tempStuffCollection = new StuffCollection();
tempStuffCollection.reset(stuffCollection.toJSON());
// do stuff here to edit the collection

so it would seem like all the references are removed... but no! when I edit the models in tempStuffCollection it still changes them in stuffCollection!

How do I seperate the two collections of models??

3 Answers 3

2

You will need to clone the collection. Here is one way to do it.

var tempStuffCollection = new StuffCollection();
stuffCollection.each(function(model) {
  tempStuffCollection.add(new Backbone.Model(model.toJSON()));
});
2
  • do you mean: tempStuffCollection.add(new Backbone.Model(model.toJSON()));
    – Kev
    Apr 18, 2013 at 19:41
  • this isnt working for me but I'm also using Backbone Relational models... I get this error:Cannot instantiate more than one Backbone.RelationalModel with the same id per type!
    – Kev
    Apr 18, 2013 at 19:50
0

Your problem seems to be that you can't have twice the same model. So you could do something like:

var tempStuffCollection = new StuffCollection();
stuffCollection.each(function(model) {
  var json = model.toJSON();
  json._id = json.id; // _id is maybe a reserved attribute, change it if needed
  delete json.id;
  tempStuffCollection.add(new Backbone.Model(json));
});

Then do the inverse operation...

0

The reason it's probably not working for you is that Backbone Relational figures out the that the models you're putting into the temp collection are the same ones in your original collection, so it uses the old models instead. It does this by looking at the idAttribute of each model.

So, you might try changing the name of the idAttribute of the models when you put them into the temp collection, then changing them back when you're done.

Maybe something like this to put them into your temp collection:

var parsedStuffCollection = stuffCollection.toJSON()

_.each(parsedStuffCollection, function(stuffAttributes){
    stuffAttributes.tempIDAttribute = stuffAttributes.myIdAttribute;
    delete stuffAttributes.myIdAttribute;
})

var tempStuffCollection = new StuffCollection({parsedStuffCollection});

Then, just do the reverse to change them back

Edit: Just realized this is doing exactly the same thing as Loamhoof's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.