开发者

Pass parameter to callback function of gadget.io.makeRequest

开发者 https://www.devze.com 2023-01-27 08:36 出处:网络
I would like to pass a parameter to the callback function of gadget.io.MakeRequest. Is this at all开发者_如何学JAVA possible?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消