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:

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

public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, … args:*) : void
  1. {
  2.  trace("AMFPHP("+serviceFunction+")");
  3.  // Create responder
  4.  var responder:Responder = new Responder(resultHandler, faultHandler);
  5.  // Create an array that will temporarily store all the arguments
  6.  var collectArgs:Array = new Array;
  7.  // Add the fixed arguments
  8.  collectArgs.push(serviceFunction);
  9.  collectArgs.push(responder);
  10.  // Loop through the optional arguments and add them too
  11.  for (var i:uint=0; i<args.length; i++)
  12.  {
  13.   collectArgs.push(args[i]);
  14.  }
  15.  // Create a reference to the function we will call
  16.  var callFunction:Function = connection.call;
  17.  // Call the function using the arguments
  18.  callFunction.apply(connection,collectArgs);
  19. }
  • Share/Bookmark

11 Comments so far »

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

  2. Dale said,

    Wrote on January 16, 2009 @ 5:44 am

    Awesome, thanks for the example!

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

  4. Pim said,

    Wrote on October 14, 2009 @ 12:13 pm

    Excellent! I really couldn’t think of this by myself :P
    Thanks for your help!

  5. amfphp said,

    Wrote on October 27, 2009 @ 5:20 am

    Are you using amfphp?? Is it good? Why do you want to use amfphp??

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

  7. InnoleDangore said,

    Wrote on December 12, 2009 @ 1:58 am

    Amazing, really awesome issue. I will blog about it also.

  8. 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, [...]

  9. Aby said,

    Wrote on March 27, 2010 @ 4:39 am

    Thanks dude. Just what I was looking for.

  10. Siddhant said,

    Wrote on May 7, 2010 @ 2:16 pm

    Never thought would get the exact solution i needed .. i was trying to right a similar class and was stuck after using args directly … thanks a lot ….

  11. Mark said,

    Wrote on July 8, 2010 @ 11:45 pm

    Awesome post – Huge thanks ! I was doing exactly the same thing (amfphp & drupal) and this had me stumped for an hour or so until I found this. Only been writing AS3 for a couple of days as I am a C++/C#/Java guy normally.

    May blog my resulting AS3 Drupal connector and publish code – so will add this blog to the code comments if thats ok.

    Thanks,
    Mark

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: