adopted-ember-addons / adopted-ember-addons/ember-changeset

Breaks with ember-data-model-fragments

オープン
#188 コメント 8 件 リアクション 1 件 担当者 0 名 GitHub で見る
bug
主要言語
JavaScript
スター
425
フォーク
136
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。