@@ -5,25 +5,35 @@ export default DS.JSONSerializer.extend({
55
66 isNewSerializerAPI : true ,
77
8- serializeHasMany : function ( snapshot , json , relationship ) {
9- var key = relationship . key ;
10-
11- if ( this . _canSerialize ( key ) ) {
12- var payloadKey ;
8+ _shouldSerializeHasMany : function ( snapshot , key , relationship ) {
9+ var relationshipType = snapshot . type . determineRelationshipType ( relationship , this . store ) ;
10+ if ( this . _mustSerialize ( key ) ) {
11+ return true ;
12+ }
13+ return this . _canSerialize ( key ) &&
14+ ( relationshipType === 'manyToNone' ||
15+ relationshipType === 'manyToMany' ||
16+ relationshipType === 'manyToOne' ) ;
17+ } ,
1318
14- // if provided, use the mapping provided by `attrs` in
15- // the serializer
16- payloadKey = this . _getMappedKey ( key ) ;
17- if ( payloadKey === key && this . keyForRelationship ) {
18- payloadKey = this . keyForRelationship ( key , "hasMany" , "serialize" ) ;
19- }
19+ // Omit the unknown hasMany relationships of pushed record
20+ // (see https://github.com/emberjs/data/issues/3760)
21+ // TODO: this override will be unecessary after merge of the following PR:
22+ // https://github.com/emberjs/data/pull/3765
23+ serializeHasMany : function ( snapshot , json , relationship ) {
24+ var key = relationship . key ;
2025
21- var relationshipType = snapshot . type . determineRelationshipType ( relationship , this . store ) ;
26+ if ( this . _shouldSerializeHasMany ( snapshot , key , relationship ) ) {
27+ var hasMany = snapshot . hasMany ( key , { ids : true } ) ;
28+ if ( hasMany !== undefined ) {
29+ // if provided, use the mapping provided by `attrs` in
30+ // the serializer
31+ var payloadKey = this . _getMappedKey ( key ) ;
32+ if ( payloadKey === key && this . keyForRelationship ) {
33+ payloadKey = this . keyForRelationship ( key , "hasMany" , "serialize" ) ;
34+ }
2235
23- if ( relationshipType === 'manyToNone' ||
24- relationshipType === 'manyToMany' ||
25- relationshipType === 'manyToOne' ) {
26- json [ payloadKey ] = snapshot . hasMany ( key , { ids : true } ) ;
36+ json [ payloadKey ] = hasMany ;
2737 // TODO support for polymorphic manyToNone and manyToMany relationships
2838 }
2939 }
@@ -91,6 +101,8 @@ export default DS.JSONSerializer.extend({
91101
92102 // Remove the undefined hasMany relationships which will fail at normalization
93103 // (see https://github.com/emberjs/data/issues/3736)
104+ // TODO: this block will be unecessary after merge of the following PR:
105+ // https://github.com/emberjs/data/pull/3747
94106 var relationshipNames = Ember . get ( primaryModelClass , 'relationshipNames' ) ;
95107 var relationships = relationshipNames . hasMany ;
96108
0 commit comments