Skip to content

Instantly share code, notes, and snippets.

@vgdub
Created June 25, 2014 17:08
Show Gist options
  • Save vgdub/425e8cf59dd8e2694e56 to your computer and use it in GitHub Desktop.
Save vgdub/425e8cf59dd8e2694e56 to your computer and use it in GitHub Desktop.
http angular inteceptor for loading spinner
angular.module('matchupApp').config ['$httpProvider', ($httpProvider) ->
$httpProvider.interceptors.push ['$log', '$rootScope', ($log, $rootScope) ->
requestCount = 0
processIsLoading = (adjustment) ->
requestCount += adjustment
isStillLoading = requestCount > 0
$rootScope.loading = isStillLoading
isStillLoading
request = (config) ->
processIsLoading 1
config
response = (promise) ->
processIsLoading -1
promise
responseError = (response) ->
$log.info 'response', response
processIsLoading -1
if response.status = '401'
window.location = "/app#/unauthorized"
return
if response.status = '500'
window.location = "/app#/servererror"
return
window.location = "/app#/unknownerror"
response
request: request
response: response
responseError: responseError
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment