@@ -13,22 +13,49 @@ export class JsonApiModel {
1313 }
1414
1515 syncRelationships ( data : any , included : any , datastore : JsonApiDatastore ) {
16+ this . parseHasMany ( data , included , datastore ) ;
17+ this . parseBelongsTo ( data , included , datastore ) ;
18+ }
19+
20+ private parseHasMany ( data : any , included : any , datastore : JsonApiDatastore ) {
1621 let hasMany = Reflect . getMetadata ( 'HasMany' , this ) ;
17- for ( let metadata of hasMany ) {
18- if ( data . relationships [ metadata . key ] ) {
19- let type = Reflect . getMetadata ( 'JsonApiDatastoreConfig' , datastore . constructor ) . models [ metadata . key ] ;
20- this [ metadata . attribute ] = this . getRelationship ( type , data , included , metadata . key ) ;
22+ if ( hasMany ) {
23+ for ( let metadata of hasMany ) {
24+ if ( data . relationships [ metadata . relationship ] && data . relationships [ metadata . relationship ] . data ) {
25+ let typeName : string = data . relationships [ metadata . relationship ] . data [ 0 ] . type ;
26+ let objectType = Reflect . getMetadata ( 'JsonApiDatastoreConfig' , datastore . constructor ) . models [ typeName ] ;
27+ this [ metadata . propertyName ] = this . getHasManyRelationship ( objectType , data , included , metadata . relationship , typeName ) ;
28+ }
29+ }
30+ }
31+ }
32+
33+ private parseBelongsTo ( data : any , included : any , datastore : JsonApiDatastore ) {
34+ let belongsTo = Reflect . getMetadata ( 'BelongsTo' , this ) ;
35+ if ( belongsTo ) {
36+ for ( let metadata of belongsTo ) {
37+ if ( data . relationships [ metadata . relationship ] && data . relationships [ metadata . relationship ] . data ) {
38+ let typeName : string = data . relationships [ metadata . relationship ] . data . type ;
39+ let objectType = Reflect . getMetadata ( 'JsonApiDatastoreConfig' , datastore . constructor ) . models [ typeName ] ;
40+ this [ metadata . propertyName ] = this . getBelongsToRelationship ( objectType , data , included , metadata . relationship , typeName ) ;
41+ }
2142 }
2243 }
2344 }
2445
25- private getRelationship ( type : { new ( data : any ) : JsonApiModel ; } , data : any , included : any , relationshipName : string ) : JsonApiModel [ ] {
46+ private getHasManyRelationship ( objectType : { new ( data : any ) : JsonApiModel ; } , data : any , included : any , relationship : string , typeName : string ) : JsonApiModel [ ] {
2647 let relationshipList : JsonApiModel [ ] = [ ] ;
27- data . relationships [ relationshipName ] . data . forEach ( ( item : any ) => {
28- let relationship : any = _ . findWhere ( included , { id : item . id } ) ;
29- relationshipList . push ( new type ( relationship ) ) ;
48+ data . relationships [ relationship ] . data . forEach ( ( item : any ) => {
49+ let relationshipData : any = _ . findWhere ( included , { id : item . id , type : typeName } ) ;
50+ relationshipList . push ( new objectType ( relationshipData ) ) ;
3051 } ) ;
3152 return relationshipList ;
3253 }
3354
55+ private getBelongsToRelationship ( objectType : { new ( data : any ) : JsonApiModel ; } , data : any , included : any , relationship : string , typeName : string ) : JsonApiModel {
56+ let id = data . relationships [ relationship ] . data . id ;
57+ let relationshipData : any = _ . findWhere ( included , { id : id , type : typeName } ) ;
58+ return new objectType ( relationshipData ) ;
59+ }
60+
3461}
0 commit comments