@@ -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 ( ) {
0 commit comments