Passing Parameters To Callback Function In DWR

DWR is one of Most Finest written libraries for web development which has made life of many developers easy. It made developers feel AJAX as Kids Langugage. It has created blanket over a rough path of Road Which Javascript Provides while manipulating Objects. Here i would explain how to pass Parameters to A callback function in DWR. I assume reader to be aware of how DWR works before reading this

I’m attaching code snippet which is self explanatory


// define an erasure function to store a reference to
// dataFromBrowser and to call dataFromServer
var callbackProxy = function(bucketMap) {
fillBucketTypes(dataFromServer, dataFromBrowser );
};

SystemDAO.getBucketTypes(callbackProxy);
}

dataFromBrowser is the parameters which is passed here
function fillBucketTypes(bucketMap,dataFromBrowser ){
//alert("In Filling "+presentBucketTypeId);
var tempBucketTypeId = "bucketType" + String(dataFromBrowser );
dwr.util.addOptions(tempBucketTypeId, bucketMap, true);
}

Parametrize Strings In Java Script

We have lots of utilities in Many Languages which can spit parametrized text. But in Javascript we dont find any such utilities. Here is one of the useful function which can do this.

Lets say We want to say Welcome Message to User and Based on User Name we want to show a warning message that account expires. Then we create a placeholder in Welcome String for user name and For nUmber of days. here is how we do in Javascript

var welcomeString = “Welcome $1. We would like to inform you that your account exipers in $2 days”;

We now use it like this
Final String would be

jstrprintf(welcomeString,”Ashwin”,1000);
This will print
Welcome Ashwin. We would like to inform you that your account exipers in 1000 days


function jstrprintf() {
len = arguments.length;
if (len == 0) { return; }
if (len == 1) { return arguments[0]; }

var result;
var regexstr;
var replstr;
var formatstr;
var re;

result = "";
regexstr = "";
replstr = "";
formatstr = arguments[0];

for (var i=1; i<arguments.length; i++) {
replstr += String(i+100) + arguments[i] + String(i + 100);
regexstr += String(i+100) + "(.*)" + String(i+100);
}
re = new RegExp(regexstr);
var result;
result = replstr.replace(re, formatstr);
return result;
}