When you clone a Backbone.Collection, are model links intact?

I .clone () is a collection so that I can use the splicing cycle on it and not interfere with the original. Are models in cloned arrays originals or copies?

I need a copy of an array with source models.

Thanks for any info!

+4
source share
1 answer

You will receive the same models as the original 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/

+8
source

Source: https://habr.com/ru/post/1479576/


All Articles