1
- /* eslint ember-suave/no-direct-property-access:1 */
2
-
3
1
import Ember from 'ember' ;
4
-
5
2
import UrlBuilder from '../utils/url-builder' ;
6
3
import normalizePayload from '../utils/normalize-payload' ;
7
4
import defaultConfig from '../config' ;
@@ -14,7 +11,9 @@ const {
14
11
ObjectProxy,
15
12
ArrayProxy,
16
13
PromiseProxyMixin,
17
- typeOf : emberTypeOf
14
+ typeOf : emberTypeOf ,
15
+ isArray,
16
+ RSVP
18
17
} = Ember ;
19
18
20
19
const promiseTypes = {
@@ -88,13 +87,27 @@ export default emberObject.extend({
88
87
} ,
89
88
90
89
_promise ( ) {
91
- return this . get ( 'adapter' ) . ajax ( this . get ( 'url' ) , this . get ( 'requestType' ) , this . get ( 'data' ) ) . then ( ( response ) => {
92
- if ( this . get ( 'config.pushToStore' ) && this . _validResponse ( response ) ) {
93
- return this . get ( 'serializer' ) . pushPayload ( this . get ( 'store' ) , response ) ;
94
- } else {
95
- return response ;
96
- }
97
- } ) ;
90
+ return this . get ( 'adapter' )
91
+ . ajax ( this . get ( 'url' ) , this . get ( 'requestType' ) , this . get ( 'data' ) )
92
+ . then ( this . _onSuccess . bind ( this ) , this . _onError . bind ( this ) ) ;
93
+ } ,
94
+
95
+ _onSuccess ( response ) {
96
+ if ( this . get ( 'config.pushToStore' ) && this . _validResponse ( response ) ) {
97
+ return this . get ( 'serializer' ) . pushPayload ( this . get ( 'store' ) , response ) ;
98
+ }
99
+
100
+ return response ;
101
+ } ,
102
+
103
+ _onError ( error ) {
104
+ if ( this . get ( 'config.pushToStore' ) && isArray ( error . errors ) ) {
105
+ let id = this . get ( 'model.id' ) ;
106
+ let typeClass = this . get ( 'model' ) . constructor ;
107
+ error . serializedErrors = this . get ( 'serializer' ) . extractErrors ( this . get ( 'store' ) , typeClass , error , id ) ;
108
+ }
109
+
110
+ return RSVP . reject ( error ) ;
98
111
} ,
99
112
100
113
_validResponse ( object ) {
0 commit comments