Passing parameters to Flex that works

Here ‘s the assignment: write a Flex application that can run against different servers (dev, uat, prod) without the need to recompile the SWF file. It does not take a rocket scientist to figure out that the URL of the server should be passed to SWF as a parameter, and we “ll do this by using a special variable flashVars in HTML wrapper. Flex documentation suggests to include flashVars parameters in the tags Object and Embed and read them using Application.application.parameters in AS3 code. At the time of this writing this does not work. But as the ancient saying goes, “Adobe closes one door but opens another rdquo;. Let “s get familiar with Flex code:

lt;?xml version= “1.0 ” encoding= “utf-8 “? gt;

lt;mx:Application xmlns:mx= “http://www.adobe.com/2006/mxml ” layout= “absolute ”

applicationComplete= “initApp() ” gt;

lt;mx:Label text=

“Will run the app deployed at http://{serverURL}:{port}/MyGreatApp.html ” / gt;

lt;mx:Script gt;

lt;![CDATA[

[Bindable]

var serverURL:String;

[Bindable]

var port:String;

function initApp():void{

serverURL=Application.application.parameters.serverURL;

port=Application.application.parameters.port

}

]] gt;

lt;/mx:Script gt;

lt;/mx:Application gt;

The script portion of this code gets the values of parameters serverURL and port (defined by us) using the Application object. We “ll add the values of these parameters to the HTML file as described below. These values are bound to the mxml label as a part of the text string.

If you “ll open generated HTML file, you “ll find the JavaScript function AC_FL_RunContent that includes flashVars parameters in a form of key-value pairs. For example, in my sample application it looks like this:

“flashvars “, ‘historyUrl=history.htm%3F amp;lconid= ‘ + lc_id + ” ”

Add our parameters serverURL and port to this string:

“flashvars “, ‘serverURL=MyDevelopmentServer amp;port=8181 amp;historyUrl=history.htm%3F amp;lconid= ‘ + lc_id

Run the application, and it “ll display the URL of the server it connects to. If you “d like to test your application against QA server, just change the values of the flashVars parameters in the HTML file.

We have one last little wrinkle to iron out: if you “ll manually change the content of the generated HTML file, next time you clean the project in Flex Builder, its content will be overwritten and you “ll lose added flashVars parameters. Here “s a simple solution to this problem: instead of adding flashVars parameters to the generated HTML, add them to the file index.template.html from the html-template directory, which Flex Builder uses for generation of run and debug versions of HTML wrapper.

Of course, this little example does not connect to any server, but it shows how to pass the server URL (or any other value) as a parameter to Flash Player, and how to assemble the URL from a mix of text and bindings

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s