Twitter: raymondcamden


Address: Lafayette, LA, USA

Followup to FlashVars in Flex Articles

01-22-2007 4,902 views Flex

Last month I wrote an article about how to use FlashVars in a Flex application. FlashVars are simple values passed in to the Flash movie from the HTML/JavaScript that embeds the SWF. I thought I'd follow it up with a practical example on an application I'm working on right now.

I'm building an administrator interface for a web site. This application will make use of Flash Remoting to talk to a CFC. The path to the CFC isn't hard coded though. To make matters worse, my dev environment isn't exactly like production. Therefore, the "dot path" location of the CFC needs to be dynamic. I decided to pass in the path via FlashVars.

I had things working on the Flex side, hard coded, so the first thing I did was to remove the source attribute from my RemoteObject tag:

view plain print about
1<mx:RemoteObject id="core" destination="ColdFusion" showBusyCursor="true" >

My Flex code was already running code on creationComplete:

view plain print about
1<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" xmlns:views="views.*" creationComplete="init()" >

So I simply updated my init() function:

view plain print about
1private var componentroot:String = "";
2
3private function init():void {
4    componentroot = this.parameters.componentroot;
5    core.source = componentroot + "components.admin";
6}

As I mentioned in the previous entry, the data from the FlashVars is available as this.parameters.name, where name is the FlashVar name.

So how do I test this? One of the cool things about FlexBuilder is that you can modify the HTML generated when you view your application. I went into the html-template folder, opened up index.template.html, and changed this line:

view plain print about
1"flashvars",'historyUrl=history.htm%3F&lconid=' + lc_id + '',

To this:

view plain print about
1"flashvars",'componentroot=secretproject.server.&historyUrl=history.htm%3F&lconid=' + lc_id + '',

This isn't the final step though. I'm deploying to a site running ColdFusion. I'll eventually change my index.html file to an index.cfm file and use an Application variable for the component root.

So now I have a problem. The this.parameters structure only exists for the core Flex file of an application, not the children. How can I get my children to also use the value? I'm thinking what I'll do is pass the value from parent to child, since that is relatively easy to do. I could make the child run code to grab it from the parent, but that is such a pain to do. (More on that later.) I don't want to have to create a custom even just to read a simple value from the parent. (But that may just be me being lazy. :)