ember-data-model-fragments
ember-data-model-fragments copied to clipboard
FragmentArray.createFragment should respect `type` in polymorphic arrays
If I have polymorphic fragment array:
export MyModel = DS.Model.extend({
parts: MF.fragmentArray('part', { polymorphic: true }),
});
with hierarchic fragment models:
export Part = MF.Fragment.extend({
// ...
}
export PartOne = Part.extend({
// ...
}
And then I want to create fragment directly on the array, with given type:
myModel.get('parts').createFragment({
type: 'part-one',
fields: ...
});
It does not respect the type passed. Rather it always creates the fragment with the type declared in the definition of the fragmentArray
. As it can be seen in the code here:
https://github.com/lytics/ember-data-model-fragments/blob/master/addon/array/fragment.js#L244
Is there reason for this? Would it make sense to look for the typeKey
in the given object to createFragment
and use that if the array definition is polymorphic?
Here's a workaround,
import FragmentArray from 'ember-data-model-fragments/array/fragment';
FragmentArray.reopen({
createFragment(props) {
const type = props?.type || this.type;
const fragment = this.owner.store.createFragment(type, props);
return this.pushObject(fragment);
},
});
@dwickern I tried to adjust the code to work in apps with both monomorphic and polymorphic fragment arrays, which now seems to work ok for my usecases:
FragmentArray.reopen({
createFragment(props) {
const record = this.owner;
let modelName = this.type;
// Determine modelName for polymorphic relationships
if (this.options.polymorphic) {
const typeKey = this.options.typeKey || 'type';
if (typeof typeKey === 'function') {
modelName = typeKey(props, record);
} else {
modelName = props[typeKey];
}
}
const fragment = record.store.createFragment(modelName, props);
return this.pushObject(fragment);
},
});
@iStefo could you PR this?