@@ -366,42 +366,32 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
366366 this . $status = 'pending' ;
367367 this . $dispatch ( 'before-request' , [ _options ] ) ;
368368
369- var dsp = this . $dispatcher ( ) , _this = this ;
370- return $http ( _options ) . then ( function ( _response ) {
371-
372- return _this . $decorate ( dsp , function ( ) {
373- if ( action && action . canceled ) {
374- // if request was canceled during request, ignore post request actions.
375- this . $status = 'canceled' ;
376- } else {
377- this . $status = 'ok' ;
378- this . $response = _response ;
379-
380- this . $dispatch ( 'after-request' , [ _response ] ) ;
381- if ( _success ) _success . call ( this , _response ) ;
382- }
383- } ) ;
384-
385- } , function ( _response ) {
386-
387- return _this . $decorate ( dsp , function ( ) {
388- if ( action && action . canceled ) {
389- // if request was canceled during request, ignore error handling
390- this . $status = 'canceled' ;
391- return this ;
392- } else {
393- this . $status = 'error' ;
394- this . $response = _response ;
395-
396- // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
397- // until the error flag is reset by user.
398-
399- this . $dispatch ( 'after-request-error' , [ _response ] ) ;
400- if ( _error ) _error . call ( this , _response ) ;
401- return $q . reject ( this ) ;
402- }
403- } ) ;
404- } ) ;
369+ return $http ( _options ) . then ( wrapPromise ( this , function ( ) {
370+ if ( action && action . canceled ) {
371+ // if request was canceled during request, ignore post request actions.
372+ this . $status = 'canceled' ;
373+ } else {
374+ this . $status = 'ok' ;
375+ this . $response = this . $last ;
376+ this . $dispatch ( 'after-request' , [ this . $last ] ) ;
377+ if ( _success ) _success . call ( this , this . $last ) ;
378+ }
379+ } ) , wrapPromise ( this , function ( ) {
380+ if ( action && action . canceled ) {
381+ // if request was canceled during request, ignore error handling
382+ this . $status = 'canceled' ;
383+ } else {
384+ this . $status = 'error' ;
385+ this . $response = this . $last ;
386+
387+ // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
388+ // until the error flag is reset by user.
389+
390+ this . $dispatch ( 'after-request-error' , [ this . $last ] ) ;
391+ if ( _error ) _error . call ( this , this . $last ) ;
392+ return $q . reject ( this ) ; // TODO: this will step over any promise generated in _error!!
393+ }
394+ } ) ) ;
405395 } ) ;
406396 } ,
407397
0 commit comments