I am using the direct web remoting library to do some ajax calls in my application. I am having a problem that I think comes down to a delayed response from the service call. Below is part of my code that I think is having the problem. The problem is in getDefaultReviewerTypeCode, the return variable isn't set in the call back till after other "stuff" processes. Is there a way to tell DWR to wait for a response before it continues processing the java script?
function makeProtocolReviewerTypesDropDown(reviewerTypes, reviewerIndex) {
var defaultReviewerType = getDefaultReviewerTypeCode();
...
var option = document.createElement('option');
option.setAttribute("value", "");
if (defaultReviewerType == '') {
option.setAttribute("selected", "selected");
}
...
for (var i = 0; i < reviewerTypes.length; i += 2) {
var reviewerType = reviewerTypes[i].replace(/^\t*/, '');
option = document.createElement('option');
option.setAttribute("value", reviewerType);
if (defaultReviewerType == reviewerType) {
option.setAttribute("selected", "selected");
}
option.text = reviewerTypes[i+1];
addSelectOption(selectElement, option);
}
return selectElement;
}
function getDefaultReviewerTypeCode() {
var defaultReviewTyper;
var dwrReply = {
callback:function(data) {
if ( data != null ) {
defaultReviewTyper = data;
} else {
defaultReviewTyper = '';
}
}
};
IacucProtocolActionAjaxService.getDefaultCommitteeReviewTypeCode(dwrReply);
return defaultReviewTyper;
}
The best way is to encapsulate all code that follows the DWR call into a separate method, and call that from the DWR callback. That way, your code that depends on the DWR results is guaranteed to be called only after DWR returns.