Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Wednesday, November 4, 2009

Pass ...rest as a parameter to another function

I recently needed to have a function that accepted a variable number of parameters. This can easily be accomplished using the function myFunction(...rest) syntax. This way I can pass in no parameters (in which case rest is an empty array), one parameter, or multiple parameters.

But, then I wanted to pass those same parameters on to another function that also accepted a variable number of parameters (e.g. ExternalInterface.call(jsFunctionName, ...args)). If you simply call the other function and pass in rest as a parameter then it won't work as expected. E.g.
    public function sayHello(...rest):void {
        // this won't work as expected
        callMe(rest);
    }
    
    private function callMe(...rest):void {
        trace(rest.length + ": ['" + rest.join("' | '""']");
    }

What happens in callMe(...rest) is rest is an array that actually contains only one item - another array which has the rest parameters that were originally passed into sayHello(...rest).
So if you made a call like this:
    sayHello("Hello""World""!");
Then the traced output would be 1: ['Hello,World,!'].
Notice that there is only 1 parameter!

The way to get around this is to use Function.apply(null, args) to call the function instead, like this:
    public function sayHello2(...rest):void {
        // use Function.apply to pass in the rest parameters properly
        var func:Function = callMe; 
        func.apply(null, rest);
    }

So if you made this call now:
    sayHello2("Hello""World""!");
Then the traced output would be 3: ['Hello' | 'World' | '!'].
Now there are 3 parameters as expected.

I found some help on this topic from The Joy Of Flex Blog by David Colleta.

Tuesday, June 30, 2009

Loading Remote SWFs and parameters

Here is another example of one SWF loading another SWF and how parameters can be passed through the url.

The main application (called Loader1) has a panel which displays information about the current application, parameters, url etc.

The main application loads a remote application SWF (called Loader2) which also has the same panel displaying information about the application, parameters, url, etc. The differences are highlighted in bold.

Here is the code for loading a remote swf - notice how parameters are passed into the url:
private function loadRemoteSWF():void {
    var remoteSwfUrl:String = 
        "http://keg.cs.uvic.ca/flexdevtips/loaders/Loader2.swf?loaderurl=hello";
    var loader:SWFLoader = new SWFLoader();
    loader.addEventListener(Event.COMPLETE, remoteSWFLoaded);
    loader.load(remoteSwfUrl);
}

private function remoteSWFLoaded(event:Event):void {
    var loader:SWFLoader = event.currentTarget as SWFLoader;
    ui.addChild(loader.content); // ui is a UIComponent
}

In the remote application you can access the parameters that were passed in from the main application through the SWFLoader by the parameters property, but you MUST be inside the remote application MXML (e.g. in this case in Loader2.mxml), otherwise if you use Application.application.parameters this will refer to the parameters from the main application and not the ones that were passed in using the SWFLoader.


Source Code: