Passing on optional arguments in ActionScript 3
The other day I ran into a small problem with optional arguments in Flex. While writing my tutorial on AMFPHP and Flex I decided to create a small static class that would take care of all the boring stuff with calling AMFPHP. This class enables me to simply use AMFPHP.send(”ServiceName”, and so on).
However, writing the class I realized I wouldn’t be able to pass on the optional parameters. If I would use:
-
function send(anArgument:Object, …args)
…I wouldn’t be able to send the args argument to the NetConnection.call method, because actionscript would send a “bunched up” array with all the arguments as ONE parameter to the call method, as opposed to a series of parameters, which was what I wanted.
The solution to this is using the Function.apply() method. What I did was to create an empty array and add a series of arguments I wanted to pass on to a function, create a reference to the function and then use the apply method. This is what my finished static method looks like:
-
{
-
trace("AMFPHP("+serviceFunction+")");
-
// Create responder
-
var responder:Responder = new Responder(resultHandler, faultHandler);
-
// Create an array that will temporarily store all the arguments
-
var collectArgs:Array = new Array;
-
// Add the fixed arguments
-
collectArgs.push(serviceFunction);
-
collectArgs.push(responder);
-
// Loop through the optional arguments and add them too
-
for (var i:uint=0; i<args.length; i++)
-
{
-
collectArgs.push(args[i]);
-
}
-
// Create a reference to the function we will call
-
var callFunction:Function = connection.call;
-
// Call the function using the arguments
-
callFunction.apply(connection,collectArgs);
-
}
Brendan said,
Wrote on September 28, 2008 @ 11:58 am
Was trying to do the exact same thing got stuck found this … BEAUTIFUL!!! Thanks a million
Dale said,
Wrote on January 16, 2009 @ 5:44 am
Awesome, thanks for the example!
sandrar said,
Wrote on September 10, 2009 @ 4:46 pm
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Pim said,
Wrote on October 14, 2009 @ 12:13 pm
Excellent! I really couldn’t think of this by myself
Thanks for your help!
amfphp said,
Wrote on October 27, 2009 @ 5:20 am
Are you using amfphp?? Is it good? Why do you want to use amfphp??
Daniel Eneström said,
Wrote on October 27, 2009 @ 10:46 am
I’ve been using AMFPHP since its “youth”, because at the beginning it was the only option I had for sending objects and arrays to and from PHP and maintaining the data format. Today there are other options, but it still works so smoothly for me, and using a wrapper that takes care of the AS-stuff I have no need to look for an alternative. Why? Do you prefer another remoting library?
InnoleDangore said,
Wrote on December 12, 2009 @ 1:58 am
Amazing, really awesome issue. I will blog about it also.
Sangpil Research» Blog Archive » using …args as function to send data to amfPHP. said,
Wrote on March 12, 2010 @ 3:40 am
[...] Links. Daniel Eneström’s blog ActionScript 3.0 References : flash_10, [...]