最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When you clone a a Backbone.Collection are the model references intact? - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked May 7, 2013 at 16:17 boomboom 11.8k9 gold badges47 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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/puleos/9bk4d/

发布评论

评论列表(0)

  1. 暂无评论