@@ -3,17 +3,15 @@ import UrlBuilder from '../utils/url-builder';
3
3
import normalizePayload from '../utils/normalize-payload' ;
4
4
import defaultConfig from '../config' ;
5
5
6
- const { assign, getOwner, computed, isArray, ArrayProxy, Object } = Ember ;
6
+ const { assign, getOwner, computed, isArray, ArrayProxy, ObjectProxy , Object } = Ember ;
7
7
8
8
export default Object . extend ( {
9
9
model : null ,
10
10
options : { } ,
11
11
payload : { } ,
12
12
instance : false ,
13
13
14
- store : computed ( 'model' , function ( ) {
15
- return this . get ( 'model' ) . store ;
16
- } ) ,
14
+ store : computed . reads ( 'model.store' ) ,
17
15
18
16
modelName : computed ( 'model' , function ( ) {
19
17
let { constructor } = this . get ( 'model' ) ;
@@ -29,22 +27,25 @@ export default Object.extend({
29
27
} ) ,
30
28
31
29
appConfig : computed ( 'model' , function ( ) {
32
- return getOwner ( this . get ( 'model' ) ) . resolveRegistration ( 'config:environment' ) . emberApiActions || { } ;
30
+ let config = getOwner ( this . get ( 'model' ) ) . resolveRegistration ( 'config:environment' ) . emberApiActions || { } ;
31
+ return Object . create ( config ) ;
33
32
} ) ,
34
33
35
- config : computed ( 'model' , 'options' , function ( ) {
36
- return assign ( defaultConfig , this . get ( 'appConfig' ) , this . get ( 'options' ) ) ;
34
+ defaultConfig : computed ( function ( ) {
35
+ return Object . create ( defaultConfig ) ;
37
36
} ) ,
38
37
39
- requestType : computed ( 'config ' , function ( ) {
40
- return this . get ( 'config' ) . type . toUpperCase ( ) ;
38
+ config : computed ( 'defaultConfig' , 'options' , 'appConfig ', function ( ) {
39
+ return Object . create ( assign ( this . get ( 'defaultConfig' ) , this . get ( 'appConfig' ) , this . get ( 'options' ) ) ) ;
41
40
} ) ,
42
41
43
- urlType : computed ( 'config' , 'requestType ', function ( ) {
44
- return this . get ( 'config' ) . urlType || this . get ( 'requestType' ) ;
42
+ requestType : computed ( 'config.type ' , function ( ) {
43
+ return this . get ( 'config.type ' ) . toUpperCase ( ) ;
45
44
} ) ,
46
45
47
- url : computed ( 'model' , 'path' , 'urlType' , 'instance' , function ( ) {
46
+ urlType : computed . or ( 'config.urlType' , 'requestType' ) ,
47
+
48
+ url : computed ( 'model' , 'path' , 'urlType' , 'instance' , 'adapter' , function ( ) {
48
49
return UrlBuilder . create ( {
49
50
path : this . get ( 'path' ) ,
50
51
adapter : this . get ( 'adapter' ) ,
@@ -54,15 +55,15 @@ export default Object.extend({
54
55
} ) . build ( ) ;
55
56
} ) ,
56
57
57
- data : computed ( 'config' , 'payload' , function ( ) {
58
- let payload = ( this . get ( 'payload' ) instanceof Object ) ? this . get ( 'payload' ) : { } ;
59
- let data = normalizePayload ( payload , this . get ( 'config' ) . normalizeOperation ) ;
60
- return assign ( this . get ( 'config' ) . ajaxOptions , { data } ) ;
58
+ data : computed ( 'config.{normalizeOperation,ajaxOptions} ' , 'payload' , function ( ) {
59
+ let payload = ( this . get ( 'payload' ) instanceof window . Object ) ? this . get ( 'payload' ) : { } ;
60
+ let data = normalizePayload ( payload , this . get ( 'config.normalizeOperation' ) ) ;
61
+ return assign ( this . get ( 'config.ajaxOptions' ) , { data } ) ;
61
62
} ) ,
62
63
63
64
callAction ( ) {
64
65
return this . get ( 'adapter' ) . ajax ( this . get ( 'url' ) , this . get ( 'requestType' ) , this . get ( 'data' ) ) . then ( ( response ) => {
65
- if ( this . get ( 'config' ) . pushToStore && response . data ) {
66
+ if ( this . get ( 'config.pushToStore' ) && response . data ) {
66
67
return this . _pushToStore ( this . get ( 'serializer' ) . pushPayload ( this . get ( 'store' ) , response ) ) ;
67
68
} else {
68
69
return response ;
@@ -71,10 +72,7 @@ export default Object.extend({
71
72
} ,
72
73
73
74
_pushToStore ( content ) {
74
- if ( isArray ( content ) ) {
75
- return ArrayProxy . create ( { content } ) ;
76
- } else {
77
- return content ;
78
- }
75
+ let proxy = isArray ( content ) ? ArrayProxy : ObjectProxy ;
76
+ return proxy . create ( { content } ) ;
79
77
}
80
78
} ) ;
0 commit comments