9

Is there a way to call a function every time after a response is returned from a server without explicitly calling it after in the callback?

The main purpose is that I do have a generic error handler service that I call in every request's callback and I want to specify it somewhere and it shall be called automatically.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96

2 Answers2

16

I gave Gloopy a +1 on solution, however, that other post he references does DOM manipulation in the function defined in the config and the interceptor. Instead, I moved the logic for starting spinner into the top of the intercepter and I use a variable in the $rootScope to control the hide/show of the spinner. It seems to work pretty well and I believe is much more testable.

<img ng-show="polling" src="images/ajax-loader.gif">
angular.module('myApp.services', ['ngResource']).
.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('myHttpInterceptor');
    var spinnerFunction = function (data, headersGetter) {
        return data;
    };
    $httpProvider.defaults.transformRequest.push(spinnerFunction);
})
//register the interceptor as a service, intercepts ALL angular ajax http calls
.factory('myHttpInterceptor', function ($q, $window, $rootScope) {
    return function (promise) {
        $rootScope.polling = true;
        return promise.then(function (response) {
            $rootScope.polling = false;
            return response;
        }, function (response) {
            $rootScope.polling = false;
            $rootScope.network_error = true;
            return $q.reject(response);
        });
    };
})
// other code left out
Community
  • 1
  • 1
Dan Doyon
  • 6,710
  • 2
  • 31
  • 40
  • Thank you very much! This is something I've been trying to do a while ago! – Tomarto Oct 17 '12 at 18:18
  • Hi, I tried this solution, but it didn't work for me. For some reason, when the proper "then" handler is invoked, it has no reference to $rootScope anymore. What am I doing wrong? – guy mograbi Apr 22 '13 at 10:41
  • Never mind - this is a strange behavior. If I debug and evaluate "rootScope" I get "no reference" error. but if I simply use it, it works.. can't explain it.. thank you - used it. works well. – guy mograbi Apr 22 '13 at 11:10
  • Actually, this solution is not working. The line $rootScope.polling = true; should be inside spinnerFunction as this is a beginning of request. – krtek May 17 '13 at 08:46
  • This code works fine for me, can you post a complete example showing how your change makes it work better? – Dan Doyon May 17 '13 at 15:53
3

If you mean for requests using $http or a $resource you can add generic error handling to responses by adding code to the $httpProvider.responseInterceptors. See more in this post.

Although it is about starting/stopping spinners using this fiddle you can add your code in the 'stop spinner' section with // do something on error. Thanks to zdam from the groups!

Community
  • 1
  • 1
Gloopy
  • 37,767
  • 15
  • 103
  • 71