ajaxdojoweb

Generic Loading Icon in Dojo


I'm giving maintenance to an application that makes several Ajax calls with:

dojo.xhrPost() and  dojo.xhrGet()

and I want to add a generic loading "icon + message" to all the calls on the system.. since the system I find difficult and low maintainable to add the loadingFunction call to all the load:function of all xhr.

So I wonder if there is a way to add a listener to all the ajax calls on the system so everytime is made a call show the loading?

I read that I can do that with aspect in 1.7 but the application I'm working on is with the Dojo Version of 1.6.

So if you know a way to show a generic message for all the ajax calls.. .


Solution

  • You can achieve this via dojo/topic, namely IO Pipeline Topics, which works since Dojo 1.4.

    See working example at jsFiddle: http://jsfiddle.net/phusick/cMHdt/

    First of all you have to globally enable IO Pipeline Topics, set ioPublish: true in one of dojoConfig, data-dojo-config or djConfig (depends on which you use).

    Then dojo.subscribe() to specific topics, e.g.:

    dojo.subscribe("/dojo/io/start", function(e) {
        dojo.style(throbberNode, "display", "block");
    });
    
    dojo.subscribe("/dojo/io/stop", function(e) {
        dojo.style(throbberNode, "display", "none");
    });
    

    Available topics according to Dojo documentation are: