Skip to content
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

Closed
DocX opened this issue Oct 15, 2018 · 3 comments · Fixed by #483
Closed

FragmentArray.createFragment should respect type in polymorphic arrays #316

DocX opened this issue Oct 15, 2018 · 3 comments · Fixed by #483

Comments

@DocX
Copy link

DocX commented Oct 15, 2018

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?

@dwickern
Copy link
Contributor

dwickern commented Oct 29, 2021

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);
  },
});

@iStefo
Copy link
Contributor

iStefo commented Apr 10, 2022

@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);
  },
});

@knownasilya
Copy link
Collaborator

@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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants