Skip to content

Instantly share code, notes, and snippets.

@grownseed
Created March 29, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grownseed/5273487 to your computer and use it in GitHub Desktop.
Save grownseed/5273487 to your computer and use it in GitHub Desktop.
angular.module('magnet.services', []).
config(['$httpProvider', function($httpProvider) {
var interceptor = ['$q', '$rootScope', function($q, $rootScope) {
//handle notifications at root
if (!$rootScope.notifications) {
$rootScope.notifications = [];
$rootScope.removeNotification = function(n) {
$rootScope.notifications = $rootScope.notifications.filter(function(notification) {
return notification != n;
});
};
}
$rootScope.loading = true;
function success(response) {
return response;
}
function error(response) {
switch (response.status) {
case 401:
window.location = '/signin';
break;
default:
//handle error notifications
if (response.data && response.data.error) {
response.data.ts = new Date().getTime();
$rootScope.notifications.push(response.data);
}
return $q.reject(response);
}
}
return function(promise) {
$rootScope.loading = true;
return promise.then(success, error).then(function(r){ $rootScope.loading = false; return r; });
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment