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.. seance the system i find difficult and low maintainable to add the loadingFunction call to all the load:function of all xhr
so I wander 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.. .
Thanks in advice..
Let's think of you are dojo.xhrGet()
.Make common method where you can pass your URl and the call function to be called.
var processDialog = new dijit.Dialog({
title : ""
}); //make it Global
function sendRequest(requestUrl, sucessFunction, errorFunction) {
dojo.xhrGet({
preventCache : "true",
url : requestUrl,
load : function (){
showProgressDlg(processDialog,true);
sucessFunction; //Call showProgressDlg(processDialog,false) once your job done inside this call back method.
},
error : function (){
showProgressDlg(processDialog,false);//If Any loading image present
errorFunction;
},
handleAs : "json"
});
}
function showProgressDlg(imgContent /*Pass Your Icon URL*/, isShow) {
if (isShow == true) {
processDialog .attr("content", imgContent);
dojo.body().appendChild(processDialog .domNode);
processDialog.titleBar.style.display = 'none';
processDialog.startup();
processDialog.show();
} else {
if (processDialog )
processDialog.hide();
}
}
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.