我正在 .clone() -ing 一个集合,这样我就可以在其上使用拼接循环而不干扰原始集合。克隆阵列中的模型是原件还是副本?
我需要的是包含原始模型的数组副本。
感谢您提供任何信息!
最佳答案
您将获得与源集合相同的模型,这些模型包含在相同类型的新集合中。
这里是collection.clone的实现:
clone: function() {
return new this.constructor(this.models);
},
或者,如果您更喜欢深度克隆,请覆盖 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();
}
}
关于javascript - 当您克隆 Backbone.Collection 时,模型引用是否完好无损?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423873/