Saturday, April 17, 2010

If you want to set any variables from html page to your Flex application. You can do it by setting FlashVars in the html page. This data can be read in Flex at runtime by accessing Application.application.parameter property.

Following example will demonstrate the steps to access the data in flex using FlashVars.

First create an Flex project in the flex builder. Now you have to set the required data in your html file in which the Flex application is reside. To do this you have to add the FlashVars property in your html.Open your index.template.html form {project}/html-template/ folder. Why this file? Modifying this file will compile your actual html page with the latest changes you have made to this template file. Doing this you will have all your modification in the final compiled html file.

Next you have to set the FlashVars property. This you have to do it at two places. First within the <object> tag you will find <embed> tag.In this tag there are few properties set for the application swf file like.src, quality, bgcolor etc. you need to add one more property here i.e.FlashVars. Now identify the data which you want to pass to Flex application. And add that in above tag property(<object><embed>) as follow.

Second place where you have to add the flash var is at javascript block. If you look above you will find javascript block with if an else if condition. in the else if condition block you will find some property is been set. Here you have to add flashVars.

Add following line of code as last parameter of AC_FL_RunContent function within else if block. "FlashVars", "Name=Shailesh Makwana&Age=32&Qualification=B'Com&Experience=10 years"

You must have noticed that the snippet of FlashVars here is little different than the first step. Also make sure you add the comma after last parameter before adding this FlashVars. This is the common mistake everyone does.

Now you are done with the html editing. When you will run the application from the builder. The output html file will have above changes.

Now, next we have to access these variables in the Flex. Open your main application MXML file. here you can access these flashVars using Application.application.parameters;. This parameter is of type object. Use these variable in your application wherever you need. I have accessed these variable in creationComplete event. And assigned the respective variable value to the text component as follow.