AJAX(Asynchronous JavaScript + XML) got its name because of its ability to interact asynchronously. This makes Ajax famous for the Rich Internet Applications aka RIAs.
But sometimes you purposefully need synchronization in your code. I needed it once. I was calling the same business method consecutively from my ajax code for two different fields. Even after synchronizing the business method and making it thread safe,I was not able to guarantee that the first method was fully finished before the second starts. Sometimes the first DWR call was processing the returned values and in the meantime second DWR call will return and mess with the original returned values. I had to synchronize the calls to make sure that both the calls get fired and completed in a proper order.
Starting from DWR1.1 you can synchronize your method calls at various levels.
I synchronized at the method level. My normal ajax code looked like myBusinessManagerImpl.getValues(‘param1’, setValuesInDWR);
Where
myBusinessManagerImpl – business layer class
getValues – the method called by Ajax
param1 – the parameter passed to the business method
setValuesInDWR – the function variable, which was defined somewhere in the code like
var setValuesInDWR = function(data){
//….DWR Code…..
};
To make the method calls synchronized I modified the method call like this
myBusinessManagerImpl.getValues(‘param1’, {callback:setValuesInDWR, async:false});
This way I was able to make two consecutive calls to the same method synchronously.
Nice one…..
solved my problem
thanks a lo
Thanks, solved my problem too