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.. .
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:
/dojo/io/start
is sent when there are no outstanding IO requests, and a new IO request is started. No arguments are passed with this topic./dojo/io/send
is sent whenever a new IO request is started. It passes the dojo.Deferred for the request with the topic./dojo/io/load
is sent whenever an IO request has loaded successfully. It passes the response and the dojo.Deferred for the request with the topic./dojo/io/error
is sent whenever an IO request has errored. It passes the error and the dojo.Deferred for the request with the topic./dojo/io/done
is sent whenever an IO request has completed, either by loading or by erroring. It passes the error and the dojo.Deferred for the request with the topic./dojo/io/stop
is sent when all outstanding IO requests have finished. No arguments are passed with this topic.