I would like to pass a parameter to the callback function of gadget.io.MakeRequest
.
Is this at all开发者_如何学JAVA possible?
function MyRequest(param){
...
gadget.io.makeRequest(url, callback, opt_params, param);
...
}
function callback(result,param)
{
eval("param(result)");
}
if (a==1)
MyRequest(mycbk1);
if (a==2)
MyRequest(mycbk2);
function mycbk1(result){
// handle results of makeRequest when a==1
}
function mycbk21(result){
// handle results of makeRequest when a==1
}
One possibility is to wrap your callback function in an anonymous function, like so:
function MyRequest(param){
...
gadget.io.makeRequest(
url,
function(result) { callback(result, param); },
opt_params);
...
}
When the gadget request finishes, the anonymous function will be called with result
as the only argument. The anonymous function subsequently calls your callback
function with both result
and param
as arguments.
精彩评论