Native Array in Emberjs does not support deep copy?

▼魔方 西西 提交于 2019-12-24 07:48:34

问题


Native Array in Emberjs does not support deep copy? I saw it just returning sliced array in the copy method. Is it a bug?


回答1:


Currently, that's an improvement going on Ember, reported by @adamjmurray: https://github.com/emberjs/ember.js/issues/588

There's also a proposition to implement it. You can use it like this:

var NativeArray = Ember.Mixin.create(Ember.NativeArray, {
    copy: function(deep) {
        if (deep) {
            return this.map(function(item){ return Ember.copy(item, true) });
        } else {
            return this.slice();
        }
    }
});

NativeArray.apply(Array.prototype);



回答2:


In Ember.Copyable the comments state that there should be a boolean parameter that if true would produce a deep copy, but obviously it's not implemented yet. You can always override it with your own method. Something like this would work for simple objects (note: untested so consider this pseudocode)

copy: function(deep) {
  if(deep) return $.extend(true, [], this);
  else return this._super();
}


来源:https://stackoverflow.com/questions/9693154/native-array-in-emberjs-does-not-support-deep-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!