a--m
a--m

Reputation: 4782

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

Upvotes: 3

Views: 772

Answers (1)

Juan Pablo Califano
Juan Pablo Califano

Reputation: 12333

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.

Upvotes: 3

Related Questions