Saturday 18 October 2014

Angular Request Count & Loading Widget (ng 1.3.0)

I my older post (AngularJS Loading Widget) I have described how to count requests and keep track of their count to be able to notify user when something gets loaded. In the new version 1.3.0 of Angular the response transformer is not called when the request fails or returns empty response. I have updated to Interceptors. I’ve avoided using them for this purpose because I was not getting constant results with them. But now I don’t know of other options. Here’s the updated code of the .config phase. Please let me know if you come across any issues. Working JSFiddle demo. If you want to know how this works, read the older post.

angular.module('myApp', [
'myApp.services',
'myApp.directives',
'myApp.controllers'])
.config(function (
$httpProvider,
requestNotificationProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
request: function (config) {
requestNotificationProvider.fireRequestStarted();
return config;
},
response: function (response) {
requestNotificationProvider.fireRequestEnded();
return response;
},

responseError: function (rejection)
{
requestNotificationProvider.fireRequestEnded();
return $q.reject(rejection);
}
}
});
});

No comments:

Post a Comment