adopted-ember-addons / adopted-ember-addons/ember-changeset
Breaks with ember-data-model-fragments
- 主要言語
- 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 はまだ評価されていません。