开发者

pass ...rest to a NetConnection call

I want to pass a rest in a netconnection call, something like this:

public function requestData(service : String, ...params) : void
{
 nc.call(service, params);
}

this doesn't work sin开发者_开发问答ce the call is expecting each parameter to be separated by commas, like:

nc.call(service, params[0], params[1], params[2]);

I've read some posts about apply, but I can't find a solution for this specific case.


Try this:

public function requestData(service : String, ...params) : void
{
    var applyArgs:Array = params && params.length > 0 
                            ? [service].concat(params) 
                            : [service];
    nc.call.apply(nc,applyArgs);    
}

I have not tested this particular piece of code, but since the second argument that Function::apply takes is an array that will be converted to a list of parameters, this should work (unless I made some silly mistake... no compiler help yet in SO!).

Basically, the applyArgs array will always contain service in its first position. If there are more extra parameteres, they'll be concatenated to this array: the result is what you pass to apply.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜