ember-data-copyable icon indicating copy to clipboard operation
ember-data-copyable copied to clipboard

Allow for copying to an object other than models

Open bgantzler opened this issue 8 years ago • 3 comments

When ever we edit a model, we make a copy first to an object, then edit that. This means that a cancel just throws the object away. To help this all computed properties are put in a mixin, only data fields are put in the model. This allows us to add the mixin to an object and simulate the model.

It would be nice to be able to use this addon to copy the data to an object instead of a new model and it looks like that could happen with a very small change where you create the model. It would have been nice if there was a method that just created the attr hash and then used that to apply. That also is easily doable except for the code at 194 using the private API. What is that accomplishing that the attrs[name] = this.get(name); didnt accomplish?

It would also be nice to expose the attrs[] because I could use that to copy the object back to the original model.

bgantzler avatar Nov 09 '17 20:11 bgantzler

This is the same reason I am currently looking into this addon. I do think that it will be helpful to have the ability to return a POJO.

@offirgolan let me know if this is something that you would be interested in and I can create a pull request.

mupkoo avatar Aug 28 '18 21:08 mupkoo

@offirgolan our team is very interested in this feature request.

In our use-case, we want to be able to copy a model's attributes, cache them for an extended period of time (local storage), and then apply the cached attributes at a later date to create a new record.

We're thinking something along the lines of the following API:

// Copy Attributes Usage
const model = this.get('store').peekRecord('user', 1);

// setting attributesOnly option will return an object of attributes
const attributes = await model
  .copy(true, { attributesOnly: true })

// applyCopyAttributes method accepts object to set attributes and create relationships
const record = await this.store.createRecord('user')
  .applyCopyAttributes(attributes)
  .save();

Would you be open to accepting a PR for this work?

flexyford avatar Apr 27 '20 20:04 flexyford

@flexyford PR is always welcome. Is applyCopyAttributes necessary? Can't you just use setProperties?

offirgolan avatar Apr 29 '20 00:04 offirgolan