-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FragmentArray.createFragment should respect type
in polymorphic arrays
#316
Comments
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? |
iStefo
added a commit
to iStefo/ember-data-model-fragments
that referenced
this issue
Apr 21, 2022
iStefo
added a commit
to iStefo/ember-data-model-fragments
that referenced
this issue
Apr 21, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I have polymorphic fragment array:
with hierarchic fragment models:
And then I want to create fragment directly on the array, with given type:
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 tocreateFragment
and use that if the array definition is polymorphic?The text was updated successfully, but these errors were encountered: