views:

56

answers:

1

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 since 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.

+1  A: 

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.

Juan Pablo Califano
dome
You're welcome. The check is there because I though params could be null, so I was trying to avoid concatenating null to applyArgs. But I've just checked and params seems to be allways an Array, with zero elements if no rest parameters were passed. So you can just do `[service].concat(params)` and remove the if.
Juan Pablo Califano