Passing external variables to Flex 4 application

Q: Why we need this?

A: Because we may need some information from server side code to initialize the client side application code developed with Flex 4.

Q: How to send these parameters to Flex 4 application?

A:You know, we embed the swf file by “embed” tag on the view page. If we set the desired parameters like traditional URL parameters on FlashVars attribute, they will be passed automatically.

<embed
allowScriptAccess=”never” src=”http://localhost:8080/projectname/clientapplicationname.swf” quality=high pluginspage=”http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash” type=”application/x-shockwave-flash”
width=”1000″ height=”600″
FlashVars=”userId=10&theme=black”>
</embed>

Emphasized with orange line, we pass the parameters.

Emphasized with green line, we should pay attention to the src attribute. If we set this field relative or just as a local file path, we possibly will get an error saying “Your application trying to access local file”. This is a Flash Player security exception. The source should be the qualified domain name. (Here, just the localhost using port 8080)

Q: How to get these parameters on Flex 4 application?

A: This is slightly different from the Flex 3 style. We use FlexGlobals that contains variables that are global to all applications within the same ApplicationDomain. We can get the top level application by its static function, then acquire the parameters like this:

var params:Object = FlexGlobals.topLevelApplication.parameters;

Here all parameters are added to the params object as attributes. We can read them simply as attributes of an object;

var userId:String = params.userId

Pay attention that, params.userId clause. Attribute name is the same as the one in FlashVars attribute.

Q: When to use it?

A: It is up to you. But I think the best way is to embed these paramters to global variables in the application after creationComplete or applicationComplete.

 

Yorum yapın

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 / Değiştir )

Twitter picture

You are commenting using your Twitter account. Log Out / Değiştir )

Facebook photo

You are commenting using your Facebook account. Log Out / Değiştir )

Connecting to %s

Follow

Get every new post delivered to your Inbox.