Duplicate an object of an Ember Data model


#1

Hi guys,

As you can see at those links:

As you can see all those questions are not answered. There is still need for documentation of code examples for copying/cloning an object in Ember Data.

What I still have so far is the following which does cloning of all attributes except hasMany items. And the drawback is that belongsTo items have to be done by hand.

var myItem= this.get('model').toJSON(),
	self = this;

// todo implement getting hasMany items

// set belongsTo items by hand
myItem['someAttr'] = this.get('someAttr');
myItem['someAttr1'] = this.get('someAttr1');
myItem['someAttr2'] = this.get('someAttr2');

var duplicatedItem= this.store.createRecord('item', myItem);

So there are some points to improve:

  1. Saving of belongsTo not by hand
  2. Saving of hasMany items

A little new bump to get attention…


#2

I was the one posting the original question at http://stackoverflow.com/questions/14912425/ember-clone-model-for-new-record

I have updated my answer there with a method I’m currently using. Maybe there’s a better way of handling the async nature of hasMany?

At least it’s handling belongsTo dynamically.