Created
March 29, 2013 20:38
-
-
Save grownseed/5273487 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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