Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've used the following angular module to create a loading screen until all $http requests finish. It works fine on angular 1.0.7, but doesn't work on angular 1.2.10 . I need to use angular-resource, and angular-route also, so I can't stay on angular 1.0.7. Can anyone help me rewrite this to work on the latest angular?

angular
.module('loadingOnAJAX', [])
.config(function($httpProvider) {
    var numLoadings = 0;
    var loadingScreen = $('<div style="position:fixed;top:0;left:0;right:0;bottom:0;z-index:10000;background-color:gray;background-color:rgba(70,70,70,0.2);"><img style="position:absolute;top:50%;left:50%;" alt="" src="loading.gif" /></div>')
        .appendTo($('body')).hide();

    $httpProvider.responseInterceptors.push(function() {
        return function(promise) {
            numLoadings++;
            loadingScreen.show();
            var hide = function(r) {
                if (!(--numLoadings)){
                    loadingScreen.hide();
                }
                return r;
            };
            return promise.then(hide, hide);
        };
    });
});

In angular 1.2.10, the loading screen doesn't appear, but If I place an alert() inside the hide function, the loading screen appears until I press OK on the alert. So I'm not sure if things are happening too fast, or what.

Corrected Version:

angular
.module('loadingOnAJAX', [])
.config(function($httpProvider) {
    var numLoadings = 0;
    var loadingScreen = $('<div style="position:fixed;top:0;left:0;right:0;bottom:0;z-index:10000;background-color:gray;background-color:rgba(70,70,70,0.2);"><img style="position:absolute;top:50%;left:50%;" alt="" src="loading.gif" /></div>')
        .appendTo($('body')).hide();

    $httpProvider.interceptors.push(function() {
        return {
            'request': function(config){
                numLoadings++;
                loadingScreen.show();
                return config;
            },
            'response': function(response){
                if (!(--numLoadings)){
                    loadingScreen.hide();
                }
                return response;
            }
        };
    });
});
share|improve this question
    
This version of an interceptor is deprecated. See docs –  calebboyd Feb 7 at 13:47
    
Thanks, I should have realized this in the first place! I've posted my new working version above, using the information at the doc link you suggested. –  Photovor Feb 7 at 14:20
add comment

2 Answers 2

angular
.module('loadingOnAJAX', [])
.config(function($httpProvider) {
var numLoadings = 0;
var loadingScreen = $('<div style="position:fixed;top:0;left:0;right:0;bottom:0;z-index:10000;background-color:gray;background-color:rgba(70,70,70,0.2);"><img style="position:absolute;top:50%;left:50%;" alt="" src="loading.gif" /></div>')
    .appendTo($('body')).hide();

$httpProvider.interceptors.push(function() {
    return {
        'request': function(config){
            numLoadings++;
            loadingScreen.show();
            return config;
        },
        'response': function(response){
            if (!(--numLoadings)){
                loadingScreen.hide();
            }
            return response;
        }
    };
});
});
share|improve this answer
add comment

Have a look at angular_busy here. Its pretty easy to implement.

Note that on the page you also find a link to Andy Joslin's excellent angular-promise-tracker. Which should be the base for your own experiments.Here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.