Techspace

IT happens only in IT

synchronization in DWR

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.

December 28, 2007 Posted by | Ajax using DWR | , , , , | 3 Comments

addOptions problem in DWR

Have you ever wondered why the addOptions is not working in DWR even after reading the specs carefully. It happened to me recently. First, of all, the API or specs or documentation for DWR(addOptions Documentation) are not detailed and sufficient. Especially, for the new and impatient learners like me, who don’t want to go through each and every page of the spec before using the technology. Second, if you are impatient and new learner then probably you might have missed some of the part. Problem: I was trying to populate the Select box from a list of Objects using the addOptions method of DWR. I have followed the documentation fully but my drop down was not being populated. (It was my second day with DWR so I didn’t know some of the complexities which ultimately solved the problem)

Solution:- I will take one example to explain this Let your select field something like this



<select id="statesList" />

And the class whose list of Objects you are trying to add is something like this



package testPackage.dwr;
public class State{
private String stateId;
private String stateName;
............
//getter and setter methods
............
}

and you want to populate this using DWR after some user action. Make sure that your callback function looks like this



var setValuesInDWR = function(data){
var fieldId = 'statesList';
var valueText = 'stateId';
var nameText = 'stateName';
dwr.util.removeAllOptions(fieldId);// step 1
dwr.util.addOptions(fieldId, data, valueText, nameText);//step 2
};


All the above steps are important

Step 1 – Actually removes the already existing values from your select list. If you don’t do this. the addOptions method will simply append the list.

Step 2 – This will actually add the values to the drop-down list.

I forgot/missed/didn’t know that you have to add the following line in dwr.xml

<convert converter=”bean” match=”testPackage.dwr.State”/>

Reason is, unless you don’t explicitly specify in your dwr.xml the user defined beans will not be converted in DWR. If your bean consists of supported datatypes which DWR can automatically convert then you can just add this declaration. After following all these steps carefully the problem of Dropdown not getting populated in DWR should be solved.

December 21, 2007 Posted by | Ajax using DWR | , , , , , | 5 Comments

   

Follow

Get every new post delivered to your Inbox.