ember-changeset icon indicating copy to clipboard operation
ember-changeset copied to clipboard

Breaks with ember-data-model-fragments

Open Baukereg opened this issue 7 years ago • 8 comments

I'm trying to implement ember-changeset (and ember-changeset-cp-validations) in a project that also uses ember-data-model-fragments (https://github.com/lytics/ember-data-model-fragments). The subobjects that this add-on creates for arrays and fragments are set on the changeset object, but of course these are still hard references to the sub objects of the actual record.

Right now I'm trying to create a workaround. This is a computed property macro that creates the changeset. It's kinda hacky, but the concept of changing the subobjects to either native arrays or Ember objects seems like the right direction to me.

import Ember from 'ember';
import createChangeset from 'ember-changeset-cp-validations';

export default function(propertyName) {
  
  return Ember.computed(propertyName, function() {
    const record = this.get(propertyName),
          fragmentKeys = Object.keys(record.get('_internalModel._fragments')),
          changeset = createChangeset(record);
          
    fragmentKeys.forEach(key => {
      const value = changeset.get(key);
      if (Ember.isArray(value)) {
        let newArray = value.toArray();
        if (Ember.typeOf(newArray[0]) === 'instance') {
          newArray = newArray.map(inst => Ember.Object.create(inst.toJSON()));
        }
        changeset.set(key, newArray);
      } else if (Ember.typeOf(value) === 'instance') {
        changeset.set(key, Ember.Object.create(value.toJSON()));
      }
    });
    
    return changeset;
  });
  
};

I still have to work this out further, but I'm thinking of creating an add-on to act as an extension to ember-changeset. But it will be tricky since ember-changeset-cp-validations already hacks into the creation of changeset objects.

Of course this is a very specific use case and I'm not even sure if this is the right place to address this issue, nor if it's reasonable to expect a solution in the ember-changeset add-on. But any suggestions on how to resolve this are welcome, and it's good to have a reference for others who might run into the same problem.

I'll keep you updated.

Baukereg avatar Jun 23 '17 19:06 Baukereg

@Baukereg Thanks for reporting the issue. I agree that this is a very niche use case (not to mention it's been awhile since you posted 😬 ), so I will close this for now. Definitely feel free to create an addon though!

nucleartide avatar Jan 11 '18 02:01 nucleartide

@nucleartide sorry to bump an old thread, but I too am trying to get ember changesets to work with ember-data-model-fragments.

Weirdly, my changeset doesn't seem to be picking up the fragment properties even though they can be accessed like any other sub property model.address.country == 'England' but changeset.address.country == undefined.

I get that you might not want to add any special hacks to get a third party addon to work but given that the addon aims to mimic regular relationships it seems odd that I'm getting nothing though in the main changeset...

fran-worley avatar Feb 11 '18 12:02 fran-worley

@fran-worley Ah interesting, will reopen this then. If possible, would you mind reproducing the issue by adding a failing test to ember-changeset? It should be possible to include ember-data-model-fragments in devDependencies for this purpose.

I'll be around for the rest of the weekend to review anything, but the upcoming week is kinda busy for me with a company hackathon coming up.

nucleartide avatar Feb 11 '18 14:02 nucleartide

I'm using ember-data-model-fragments as well, it's a very popular addon. It has a bit more GitHub stars than ember-changeset, ~30% more npm installs and twice the number of GitHub observers.

I believe the compatibility between the two addons is important.

lolmaus avatar Mar 24 '18 12:03 lolmaus

+1, same here (using ember-data-model-fragments and ember-changeset)

roberkules avatar Jun 19 '18 22:06 roberkules

is someone working on this ?

curtu avatar Oct 23 '18 11:10 curtu

I know this is an old thread. But I might ensure compatibility in the near future...I'll try to remember to get to it later this month.

snewcomer avatar Nov 06 '19 04:11 snewcomer

If anybody has the bandwidth to try out the v3.0 series with ember-data-model-fragments, that would be 👍. Definitely want to ensure compatibility. This was a major rewrite, so I am hopeful something has changed.

snewcomer avatar Feb 12 '20 17:02 snewcomer