Skip to content

Instantly share code, notes, and snippets.

@zivester
Created March 28, 2013 19:35
Show Gist options
  • Select an option

  • Save zivester/5266158 to your computer and use it in GitHub Desktop.

Select an option

Save zivester/5266158 to your computer and use it in GitHub Desktop.
httpInterceptor for broadcasting open events
.factory('httpInterceptor', function($q, $injector, $rootScope){
var $http;
function open(num){
$rootScope.$broadcast('httpOpen', num);
}
function close(num){
$rootScope.$broadcast('httpClose', num);
}
return function(promise){
$http || ($http = $injector.get('$http'));
open($http.pendingRequests.length);
return promise.then(
function(response){
close($http.pendingRequests.length);
return response;
},
function(response){
close($http.pendingRequests.length);
return $q.reject(response);
}
);
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment