Here's an easy way to clone your Ember model with relationships. works great.
Create a copy method, for example,
import Ember from 'ember'; export default Ember.Mixin.create(Ember.Copyable, { copy(deepClone) { var model = this, attrs = model.toJSON(), class_type = model.constructor; var root = Ember.String.decamelize(class_type.toString().split(':')[1]); if(deepClone) { this.eachRelationship(function(key, relationship){ if (relationship.kind == 'belongsTo') { attrs[key] = model.get(key).copy(true); } else if(relationship.kind == 'hasMany' && Ember.isArray(attrs[key])) { attrs[key].splice(0); model.get(key).forEach(function(obj) { attrs[key].addObject(obj.copy(true)); }); } }); } return this.store.createRecord(root, attrs); } });
Add mixin to your model,
Note. If you want to clone your child model, you also need to include mixin in the child model
APPLICATION:
Akilan M
source share