Nailing the A in AJAX
There has been some debate recently about the A in AJAX and how much harder it makes life. There is a feature of DWR that does a long way to nailing the problem without hanging the UI thread which I guess people don't know about. Yet...
The secret sauce is DWREngine.setOrdered(true). Which does what it says on the tin - makes sure that we don't dispatch a new Ajax call until the last one has returned.
If you've made a call to DWREngine.setOrdered(true) then DWR keeps a list of the calls that it has been asked to send, but has not been able to yet and sends them just as soon as it can. (So whenever an Ajax call returns DWR checks to see if it needs to send another one out).
It is documented (See the section on "Call Ordering").
The implementation is a work of genius. Someone sent me the code shortly after I pronounced that it couldn't be done so it is neat.
Whilst it is an easy solution that doesn't require much effort, I would generally use the closure solution instead because it isn't much harder to implement and it does not have the draw-backs in responsiveness.
Another DWR feature for UI responsiveness
While we are on the subject of DWR features that are not commonly known, a related function is the GMail style loading message that lets users know that some background work is going on.
Just call DWRUtil.useLoadingMessage(); from the onload event and you get a red "Loading" message in the top right corner. All the demos at the DWR site use this option.
Re: Nailing the A in AJAX
For example, something like:
var currentCommand;
function onKeyPress()
{
if ( currentCommand != null )
currentCommand.cancel();
// the key bit - getting handle on current command
currentCommand=JMyObject.doSearch(replyfunc, text);
}
var replyfunc = function(data)
{
// display results
}
Thanks, Alfie.ps. just found DWR tonight and very impressed!
Re: Nailing the A in AJAX
There isn't, you can ignore the results when they come back fairly easily, and you can use timeouts in DWR to give up if the server has been too long, which is a related function.
I've raised the following enhancement issue: https://dwr.dev.java.net/issues/show_bug.cgi?id=75