Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When you clone a a Backbone.Collection are the model references intact?

I'm .clone() -ing a collection so that I can use a splice loop on it and not interfere with the original. Are the models in cloned array originals or copies?

What I need is a copy of the array with the original models in it.

Thanks for any info!

like image 427
boom Avatar asked Feb 16 '23 14:02

boom


1 Answers

You will get the same models as the source collection wrapped in a new collection of the same type.

Here is the implementation of collection.clone:

   clone: function() {
      return new this.constructor(this.models);
    },

Or if you prefer a deep clone, override Backbone.Collection.clone

clone: function(deep) {
  if(deep) {
    return new this.constructor(_.map(this.models, function(m) { return m.clone(); }));
  }else{
    return Backbone.Collection.prototype.clone();
  }
}

http://jsfiddle.net/puleos/9bk4d/

like image 137
Scott Puleo Avatar answered Apr 27 '23 01:04

Scott Puleo