//intercepts all inbound/outbound ajax data evoapp.factory('httpInterceptor', function ($rootScope, $q){ //http://endlessindirection.wordpress.com/2013/05/19/angularjs-global-loading-message-with-http-interceptors/ var activeRequests = 0; var started = function(broadcastEvents){ if(activeRequests == 0 && broadcastEvents === true) { $rootScope.$broadcast('loadingStatusActive'); } activeRequests++; }; var ended = function(response){ //intercept exceptions if(response.data.success != undefined) { if(response.data.messages.length > 0) { var firstMessage = response.data.messages[0]; if(firstMessage.type == 'SESSIONTIMEOUT') { $rootScope.$broadcast('onsessiontimeout', {response: response.data}); } } } activeRequests--; if(activeRequests==0) { $rootScope.$broadcast('loadingStatusInactive'); } }; return { request: function (config){ started(config.$$broadcastEvents); return config || $q.when(config); }, // On request failure requestError: function (rejection){ ended(rejection); return $q.reject(rejection); }, // On response success response: function (response){ ended(response); return response || $q.when(response); }, // On response failure responseError: function (rejection){ ended(rejection); return $q.reject(rejection); } }; }); //instantiate httpInterceptor evoapp.config(function ($httpProvider){ $httpProvider.interceptors.push('httpInterceptor'); });