Skip to content

Commit

Permalink
fix(polymorphism): fix regression introduced with ember-data-model-fr…
Browse files Browse the repository at this point in the history
…agments 6.0.0 where owner is not passed to typeKey function (#474)

* fix(polymorphism): fix regression introduced with ember-data-model-fragments 6.0.0 where owner is not passed to typeKey function

* self review fix

---------

Co-authored-by: Vincent Molinié <[email protected]>
  • Loading branch information
VincentMolinie and Vincent Molinié authored Jul 15, 2023
1 parent 1639f70 commit 34fe71f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions addon/record-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ export default class FragmentRecordData extends RecordData {
const type = getActualFragmentType(
definition.modelName,
definition.options,
attributes
attributes,
this._fragmentGetRecord()
);
const recordData = this.storeWrapper.recordDataFor(type);
recordData.setFragmentOwner(this, definition.name);
Expand Down Expand Up @@ -706,12 +707,13 @@ export default class FragmentRecordData extends RecordData {

pushData(data, calculateChange) {
let changedFragmentKeys;

const subFragmentsToProcess = [];
if (data.attributes) {
// copy so that we don't mutate the caller's data
const attributes = Object.assign({}, data.attributes);
data = Object.assign({}, data, { attributes });

const newCanonicalFragments = {};
for (const [key, behavior] of Object.entries(this._fragmentBehavior)) {
const canonical = data.attributes[key];
if (canonical === undefined) {
Expand All @@ -720,12 +722,24 @@ export default class FragmentRecordData extends RecordData {
// strip fragments from the attributes so the super call does not process them
delete data.attributes[key];

subFragmentsToProcess.push({ key, behavior, canonical, attributes });
}
}

// Wee need first the attributes to be setup before the fragment, to be able to access them (for polymorphic fragments for example)
const changedAttributeKeys = super.pushData(data, calculateChange);

if (data.attributes) {
const newCanonicalFragments = {};

subFragmentsToProcess.forEach(({ key, behavior, canonical }) => {
const current =
key in this._fragmentData
? this._fragmentData[key]
: this._getFragmentDefault(key);
newCanonicalFragments[key] = behavior.pushData(current, canonical);
}
});

if (calculateChange) {
changedFragmentKeys = this._changedFragmentKeys(newCanonicalFragments);
}
Expand All @@ -737,13 +751,12 @@ export default class FragmentRecordData extends RecordData {
);
}

const changedAttributeKeys = super.pushData(data, calculateChange);
const changedKeys = mergeArrays(changedAttributeKeys, changedFragmentKeys);
if (gte('ember-data', '4.5.0') && changedKeys?.length > 0) {
internalModelFor(this).notifyAttributes(changedKeys);
}
// on ember-data 2.8 - 4.4, InternalModel.setupData will notify
return changedKeys;
return changedKeys || [];
}

willCommit() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/serialize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module('unit - Serialization', function (hooks) {
store = null;
});

test.skip('polymorphic properties are deserialized correctly', async function (assert) {
test('polymorphic properties are deserialized correctly', async function (assert) {
store.pushPayload('component', {
data: [
{
Expand Down

0 comments on commit 34fe71f

Please sign in to comment.