The No.1 i-Technology Magazine in the World !
   
 

      ES no BS
       My son's animations and music            training         Twitter            No BS IT podcast

Archives

««Mar 2010»»
SMTWTFS
  123456
78
9
10
11
12
13
14
1516
17
181920
21222324252627
28293031

RSS Feed








Subscribe to these blogs

Passing parameters to Flex that works

posted Thursday, 12 October 2006

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”.  Let’s get familiar with Flex code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    applicationComplete="initApp()">
    
  <mx:Label text=
"Will run the app deployed at http://{serverURL}:{port}/MyGreatApp.html" />
  <mx:Script>
      <![CDATA[
          [Bindable]
          var serverURL:String;
          
          [Bindable]
          var port:String;
          
          function initApp():void{
              serverURL=Application.application.parameters.serverURL;
              port=Application.application.parameters.port
          }
      ]]>
  </mx:Script>
</mx:Application>


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&lconid=' + lc_id +’’

Add our parameters serverURL and port to this string:

"flashvars",'serverURL=MyDevelopmentServer&port=8181&historyUrl=history.htm%3F&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

 

tags:          

links: digg this    del.icio.us    technorati    




1. Gene left...
Tuesday, 24 October 2006 1:30 pm

Thanks this works Great. All the Flex documents are not correct on how to pass parameters or form variable.


2. Jeff left...
Monday, 6 November 2006 3:26 pm :: http://www.jeffdouglas.com/blog

Thanks for the help! Adobe's docs didn't prove very useful.


3. Jeff Anderson left...
Tuesday, 5 June 2007 12:09 am

Awesome, I am fairly new to flex and getting pretty good, this challenge stumped me, and no other tutorials gave a true correct syntax for me. Thanks


4. sean2078 left...
Tuesday, 5 June 2007 11:31 am

Thanks for the tip! Really helped out!


5. Jason The Saj left...
Tuesday, 5 June 2007 1:51 pm

I got this implemented. But I am curious how to get it to work with mx:HTTPService as the URL property.

Anyone manage to bring it to this next step?


6. Yakov Fain left...
Tuesday, 5 June 2007 2:37 pm

Just use binding URL="{myvar}"


7. Arul Prasad left...
Monday, 30 July 2007 1:30 pm :: http://arulprasad.blogspot.com

Most probably you have figured this out by now, I tbought i'd add this however, because it might be misleading for anyone who comes searching into this blog post.

When you said you added flashvars to the object and embed tags, I assume, you added to them in the noscript portion of the html, which ends up never being used, unless you had js turned off in ur browser. thats the reason adding it to the js function worked, because that portion of ur html is what is actually being used.

So make sure u add ur flashvars in the html page - both at the no script section and also in the js. also Application.appliication.parameters can actuallly read query string params sent to the html page. So u can just pass params to the html and they will be passed on to the swf.


8. Michael N left...
Monday, 15 October 2007 10:34 am

It might be useful to mention that inside classes you should import mx.core.Application to accomplish the same goals