Skip to content

Commit 34fe71f

Browse files
VincentMolinieVincent Molinié
andauthored
fix(polymorphism): fix regression introduced with ember-data-model-fragments 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]>
1 parent 1639f70 commit 34fe71f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

addon/record-data.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ export default class FragmentRecordData extends RecordData {
604604
const type = getActualFragmentType(
605605
definition.modelName,
606606
definition.options,
607-
attributes
607+
attributes,
608+
this._fragmentGetRecord()
608609
);
609610
const recordData = this.storeWrapper.recordDataFor(type);
610611
recordData.setFragmentOwner(this, definition.name);
@@ -706,12 +707,13 @@ export default class FragmentRecordData extends RecordData {
706707

707708
pushData(data, calculateChange) {
708709
let changedFragmentKeys;
710+
711+
const subFragmentsToProcess = [];
709712
if (data.attributes) {
710713
// copy so that we don't mutate the caller's data
711714
const attributes = Object.assign({}, data.attributes);
712715
data = Object.assign({}, data, { attributes });
713716

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

725+
subFragmentsToProcess.push({ key, behavior, canonical, attributes });
726+
}
727+
}
728+
729+
// Wee need first the attributes to be setup before the fragment, to be able to access them (for polymorphic fragments for example)
730+
const changedAttributeKeys = super.pushData(data, calculateChange);
731+
732+
if (data.attributes) {
733+
const newCanonicalFragments = {};
734+
735+
subFragmentsToProcess.forEach(({ key, behavior, canonical }) => {
723736
const current =
724737
key in this._fragmentData
725738
? this._fragmentData[key]
726739
: this._getFragmentDefault(key);
727740
newCanonicalFragments[key] = behavior.pushData(current, canonical);
728-
}
741+
});
742+
729743
if (calculateChange) {
730744
changedFragmentKeys = this._changedFragmentKeys(newCanonicalFragments);
731745
}
@@ -737,13 +751,12 @@ export default class FragmentRecordData extends RecordData {
737751
);
738752
}
739753

740-
const changedAttributeKeys = super.pushData(data, calculateChange);
741754
const changedKeys = mergeArrays(changedAttributeKeys, changedFragmentKeys);
742755
if (gte('ember-data', '4.5.0') && changedKeys?.length > 0) {
743756
internalModelFor(this).notifyAttributes(changedKeys);
744757
}
745758
// on ember-data 2.8 - 4.4, InternalModel.setupData will notify
746-
return changedKeys;
759+
return changedKeys || [];
747760
}
748761

749762
willCommit() {

tests/unit/serialize_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module('unit - Serialization', function (hooks) {
2424
store = null;
2525
});
2626

27-
test.skip('polymorphic properties are deserialized correctly', async function (assert) {
27+
test('polymorphic properties are deserialized correctly', async function (assert) {
2828
store.pushPayload('component', {
2929
data: [
3030
{

0 commit comments

Comments
 (0)