@@ -35,6 +35,11 @@ export default class Model extends StaticModel {
35
35
return Model . $http
36
36
}
37
37
38
+ config ( config = { } ) {
39
+ this . _config = config
40
+ return this
41
+ }
42
+
38
43
resource ( ) {
39
44
return `${ this . constructor . name . toLowerCase ( ) } s`
40
45
}
@@ -276,6 +281,16 @@ export default class Model extends StaticModel {
276
281
}
277
282
}
278
283
284
+ _reqConfig ( config = { } , options = { forceMethod : false } ) {
285
+ const _config = { ...config , ...this . _config }
286
+
287
+ if ( options . forceMethod ) {
288
+ _config . method = config . method
289
+ }
290
+
291
+ return _config
292
+ }
293
+
279
294
first ( ) {
280
295
return this . get ( ) . then ( response => {
281
296
let item
@@ -303,10 +318,12 @@ export default class Model extends StaticModel {
303
318
let base = this . _fromResource || `${ this . baseURL ( ) } /${ this . resource ( ) } `
304
319
let url = `${ base } /${ identifier } ${ this . _builder . query ( ) } `
305
320
306
- return this . request ( {
307
- url,
308
- method : 'GET'
309
- } ) . then ( response => {
321
+ return this . request (
322
+ this . _reqConfig ( {
323
+ url,
324
+ method : 'GET'
325
+ } )
326
+ ) . then ( response => {
310
327
return this . _applyInstance ( response . data )
311
328
} )
312
329
}
@@ -326,10 +343,12 @@ export default class Model extends StaticModel {
326
343
base = this . _customResource ? `${ this . baseURL ( ) } /${ this . _customResource } ` : base
327
344
let url = `${ base } ${ this . _builder . query ( ) } `
328
345
329
- return this . request ( {
330
- url,
331
- method : 'GET'
332
- } ) . then ( response => {
346
+ return this . request (
347
+ this . _reqConfig ( {
348
+ url,
349
+ method : 'GET'
350
+ } )
351
+ ) . then ( response => {
333
352
let collection = this . _applyInstanceCollection ( response . data )
334
353
335
354
if ( response . data . data !== undefined ) {
@@ -357,32 +376,38 @@ export default class Model extends StaticModel {
357
376
throw new Error ( 'This model has a empty ID.' )
358
377
}
359
378
360
- return this . request ( {
361
- url : this . endpoint ( ) ,
362
- method : 'DELETE'
363
- } ) . then ( response => response )
379
+ return this . request (
380
+ this . _reqConfig ( {
381
+ method : 'DELETE' ,
382
+ url : this . endpoint ( )
383
+ } )
384
+ ) . then ( response => response )
364
385
}
365
386
366
387
save ( ) {
367
388
return this . hasId ( ) ? this . _update ( ) : this . _create ( )
368
389
}
369
390
370
391
_create ( ) {
371
- return this . request ( {
372
- method : 'POST' ,
373
- url : this . endpoint ( ) ,
374
- data : this
375
- } ) . then ( response => {
392
+ return this . request (
393
+ this . _reqConfig ( {
394
+ method : 'POST' ,
395
+ url : this . endpoint ( ) ,
396
+ data : this
397
+ } , { forceMethod : true } )
398
+ ) . then ( response => {
376
399
return this . _applyInstance ( response . data . data || response . data )
377
400
} )
378
401
}
379
402
380
403
_update ( ) {
381
- return this . request ( {
382
- method : 'PUT' ,
383
- url : this . endpoint ( ) ,
384
- data : this
385
- } ) . then ( response => {
404
+ return this . request (
405
+ this . _reqConfig ( {
406
+ method : 'PUT' ,
407
+ url : this . endpoint ( ) ,
408
+ data : this
409
+ } )
410
+ ) . then ( response => {
386
411
return this . _applyInstance ( response . data . data || response . data )
387
412
} )
388
413
}
@@ -392,18 +417,22 @@ export default class Model extends StaticModel {
392
417
*/
393
418
394
419
attach ( params ) {
395
- return this . request ( {
396
- method : 'POST' ,
397
- url : this . endpoint ( ) ,
398
- data : params
399
- } ) . then ( response => response )
420
+ return this . request (
421
+ this . _reqConfig ( {
422
+ method : 'POST' ,
423
+ url : this . endpoint ( ) ,
424
+ data : params
425
+ } )
426
+ ) . then ( response => response )
400
427
}
401
428
402
429
sync ( params ) {
403
- return this . request ( {
404
- method : 'PUT' ,
405
- url : this . endpoint ( ) ,
406
- data : params
407
- } ) . then ( response => response )
430
+ return this . request (
431
+ this . _reqConfig ( {
432
+ method : 'PUT' ,
433
+ url : this . endpoint ( ) ,
434
+ data : params
435
+ } )
436
+ ) . then ( response => response )
408
437
}
409
438
}
0 commit comments