Last active
December 21, 2015 11:29
-
-
Save BrianGenisio/6299303 to your computer and use it in GitHub Desktop.
The default behavior is to reject the error. If you don't do that, you are hiding the error condition.
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('httpProgress') | |
.factory('httpProgressInterceptor', function($q) { | |
var requestCount = 0; | |
function updateProgress(update) { | |
var previous = requestCount; | |
requestCount += update; | |
if(previous <= 0 && requestCount > 0) { | |
NProgress.start(); | |
} else if (previous > 0 && requestCount <= 0) { | |
NProgress.done(true); | |
} else { | |
NProgress.inc(); | |
} | |
} | |
function incrementRequests() { | |
updateProgress(1); | |
} | |
function decrementRequests() { | |
updateProgress(-1); | |
} | |
return { | |
request: function (request) { | |
incrementRequests(); | |
return request; | |
}, | |
response: function (response) { | |
decrementRequests(); | |
return response; | |
}, | |
responseError: function (rejection) { | |
decrementRequests(); | |
return $q.reject(rejection); | |
} | |
}; | |
}) | |
.config(function ($httpProvider) { | |
$httpProvider.interceptors.push('httpProgressInterceptor'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment