adopted-ember-addons / ember-changeset

Ember.js flavored changesets, inspired by Ecto
http://bit.ly/ember-changeset-demo
MIT License
431 stars 141 forks source link

Breaks with ember-data-model-fragments #188

Open Baukereg opened 7 years ago

Baukereg commented 7 years ago

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.

nucleartide commented 6 years ago

@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!

fran-worley commented 6 years ago

@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...

nucleartide commented 6 years ago

@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.

lolmaus commented 6 years ago

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.

roberkules commented 6 years ago

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

curtu commented 5 years ago

is someone working on this ?

snewcomer commented 4 years ago

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 commented 4 years ago

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.