@@ -10,6 +10,7 @@ export default class Model extends StaticModel {
1010 this . _builder = new Builder ( this )
1111 } else {
1212 Object . assign ( this , ...attributes )
13+ this . _applyRelations ( this )
1314 }
1415
1516 if ( this . baseURL === undefined ) {
@@ -123,6 +124,10 @@ export default class Model extends StaticModel {
123124 return this
124125 }
125126
127+ relations ( ) {
128+ return { }
129+ }
130+
126131 /**
127132 * Helpers
128133 */
@@ -226,6 +231,48 @@ export default class Model extends StaticModel {
226231 * Result
227232 */
228233
234+ _applyInstance ( data , model = this . constructor ) {
235+ const item = new model ( data )
236+
237+ if ( this . _fromResource ) {
238+ item . _from ( this . _fromResource )
239+ }
240+
241+ return item
242+ }
243+
244+ _applyInstanceCollection ( data , model = this . constructor ) {
245+ let collection = data . data || data
246+ collection = Array . isArray ( collection ) ? collection : [ collection ]
247+
248+ collection = collection . map ( c => {
249+ return this . _applyInstance ( c , model )
250+ } )
251+ return collection
252+ }
253+
254+ _applyRelations ( model ) {
255+ const relations = model . relations ( )
256+
257+ for ( const relation of Object . keys ( relations ) ) {
258+ if ( ! model [ relation ] ) {
259+ return ;
260+ }
261+
262+ if ( Array . isArray ( model [ relation ] . data ) || Array . isArray ( model [ relation ] ) ) {
263+ const collection = this . _applyInstanceCollection ( model [ relation ] , relations [ relation ] )
264+
265+ if ( model [ relation ] . data !== undefined ) {
266+ model [ relation ] . data = collection
267+ } else {
268+ model [ relation ] = collection
269+ }
270+ } else {
271+ model [ relation ] = this . _applyInstance ( model [ relation ] , relations [ relation ] )
272+ }
273+ }
274+ }
275+
229276 first ( ) {
230277 return this . get ( ) . then ( response => {
231278 let item
@@ -257,13 +304,7 @@ export default class Model extends StaticModel {
257304 url,
258305 method : 'GET'
259306 } ) . then ( response => {
260- const item = new this . constructor ( response . data )
261-
262- if ( this . _fromResource ) {
263- item . _from ( this . _fromResource )
264- }
265-
266- return item
307+ return this . _applyInstance ( response . data )
267308 } )
268309 }
269310
@@ -274,7 +315,7 @@ export default class Model extends StaticModel {
274315
275316 return this
276317 . find ( identifier )
277- . then ( response => new this . constructor ( response . data || response ) )
318+ . then ( response => this . _applyInstance ( response . data || response ) )
278319 }
279320
280321 get ( ) {
@@ -286,18 +327,7 @@ export default class Model extends StaticModel {
286327 url,
287328 method : 'GET'
288329 } ) . then ( response => {
289- let collection = response . data . data || response . data
290- collection = Array . isArray ( collection ) ? collection : [ collection ]
291-
292- collection = collection . map ( c => {
293- let item = new this . constructor ( c )
294-
295- if ( this . _fromResource ) {
296- item . _from ( this . _fromResource )
297- }
298-
299- return item
300- } )
330+ let collection = this . _applyInstanceCollection ( response . data )
301331
302332 if ( response . data . data !== undefined ) {
303333 response . data . data = collection
@@ -340,8 +370,7 @@ export default class Model extends StaticModel {
340370 url : this . endpoint ( ) ,
341371 data : this
342372 } ) . then ( response => {
343- let self = Object . assign ( this , response . data )
344- return self
373+ return this . _applyInstance ( response . data )
345374 } )
346375 }
347376
@@ -351,8 +380,7 @@ export default class Model extends StaticModel {
351380 url : this . endpoint ( ) ,
352381 data : this
353382 } ) . then ( response => {
354- let self = Object . assign ( this , response . data )
355- return self
383+ return this . _applyInstance ( response . data )
356384 } )
357385 }
358386
@@ -375,5 +403,4 @@ export default class Model extends StaticModel {
375403 data : params
376404 } ) . then ( response => response )
377405 }
378-
379406}
0 commit comments