7 Replies Latest reply on Aug 15, 2006 6:17 AM by francois-yanick

    Passing Parameter(s) to the fLex SWF Compiled Object

    francois-yanick
      I recently found that by copying all the HTML source generated by Flex into my own CFM page, I was able to add some server-side process.

      Now, I ask myself if it is possible to pass a parameter - or more than one - to the SWF object generated and compiled with Flex?

      In example; let's say I work in ColdFusion with a Session scope variable to store the current navigation language of the user. I would like to transmit this information into the SWF (Flex) in order to use this information through the different Flex components I use in the same SWF.

      I found some little help from the NET that was talking about flashvars. I do not really understand how to use this as there is no name for the parameter-variable.

      I also found another link that was talking about adding parameter at the end of the filename of the SWF:

      <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      id="accueil" width="100%" height="100%"
      codebase=" http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
      <param name="movie" value="accueil.swf?SessionLang=#session.lang#" />
      <param name="quality" value="high" />
      <param name="bgcolor" value="#869ca7" />
      <param name="allowScriptAccess" value="sameDomain" />
      <embed src="accueil.swf?SessionLang=#session.lang#" quality="high" bgcolor="#869ca7"
      width="100%" height="100%" name="accueil" align="middle"
      play="true"
      loop="false"
      allowScriptAccess="sameDomain"
      type="application/x-shockwave-flash"
      pluginspage=" http://www.adobe.com/go/getflashplayer">
      </embed>
      </object>

      This is what I tried but by forcing another value for Session.Lang, nothing happen. I am suspicious that it is really work that way!

      If anybody have information about passing parameter to a Flex SWF object, please let me know.
        • 1. Re: Passing Parameter(s) to the fLex SWF Compiled Object
          Matlus Level 1
          You should be able to use flashvars for this. It means you have to modify the javascript of the html page hosting the flash player.

          There is a JavaScript method called AC_FL_RunContent that needs to be modified like so:

          NOTE: This is not a ColdFusion example, so please make changes accordingly.

          AC_FL_RunContent(
          "src", "Flex/CorporationContactDataEntry",
          "width", "100%",
          "height", "100%",
          "align", "middle",
          "id", "CorporationContactDataEntry",
          "quality", "high",
          "bgcolor", "#869ca7",
          "name", "CorporationContactDataEntry",
          "flashvars","<%# FlashVars %>",
          "allowScriptAccess","sameDomain",
          "type", "application/x-shockwave-flash",
          "pluginspage", " http://www.adobe.com/go/getflashplayer"
          );

          the tag <%#FlashVars %> eventually gets resolved to a string of name/value pairs like so:

          name1=value1&name2=value2&name3=value3

          In your Flex Application, you then have access to these parameters like so:

          Application.application.parameters.name1

          and so on.

          Shiv.
          • 2. Re: Passing Parameter(s) to the fLex SWF Compiled Object
            francois-yanick Level 1
            First of all, thank you for your hint. It seems to be okay as it is not generated any errors. But, in fact, it is not seems to accept the switching of the navigation language. I tried to force the English as the French is by default and the Flex SWF always displays the elements in french.

            Now, is it something I will have to "refresh" from the Flex Application Scope in order to consider the new value of the parameters? Maybe the Flex SWF has is own session scope - completely separate from the ColdFusion's one!!!

            Here is my source page for the ColdFusion page:

            Accueil.cfm

            <cfif NOT IsDefined("session.lang") OR NOT IsDefined("session.language")>
            <cfset session.lang = "fr">
            <cfset session.language = 1>
            </cfif>
            <cfset session.lang = "en">
            <cfset session.language = 2>
            <!-- saved from url=(0014)about:internet -->
            <html lang="en">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Mush Hi-Tech</title>
            <script src="AC_OETags.js" language="javascript"></script>
            <style>
            body { margin: 0px; overflow:hidden }
            </style>
            <script language="JavaScript" type="text/javascript">
            <!--
            // -----------------------------------------------------------------------------
            // Globals
            // Major version of Flash required
            var requiredMajorVersion = 9;
            // Minor version of Flash required
            var requiredMinorVersion = 0;
            // Minor version of Flash required
            var requiredRevision = 0;
            // -----------------------------------------------------------------------------
            // -->
            </script>
            </head>

            <body scroll="no">
            <script language="JavaScript" type="text/javascript" src="history.js"></script>
            <script language="JavaScript" type="text/javascript">
            <!--
            // Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
            var hasProductInstall = DetectFlashVer(6, 0, 65);

            // Version check based upon the values defined in globals
            var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);


            // Check to see if a player with Flash Product Install is available and the version does not meet the requirements for playback
            if ( hasProductInstall && !hasRequestedVersion ) {
            // MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
            // This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
            // DO NOT MODIFY THE FOLLOWING FOUR LINES
            // Location visited after installation is complete if installation is required
            var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
            var MMredirectURL = window.location;
            document.title = document.title.slice(0, 47) + " - Flash Player Installation";
            var MMdoctitle = document.title;

            AC_FL_RunContent(
            "src", "playerProductInstall",
            "FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"" ,
            "width", "100%",
            "height", "100%",
            "align", "middle",
            "id", "accueil",
            "quality", "high",
            "bgcolor", "#869ca7",
            "name", "accueil",
            "allowScriptAccess","sameDomain",
            "type", "application/x-shockwave-flash",
            "pluginspage", " http://www.adobe.com/go/getflashplayer"
            );
            } else if (hasRequestedVersion) {
            // if we've detected an acceptable version
            // embed the Flash Content SWF when all tests are passed
            AC_FL_RunContent(
            "src", "accueil",
            "width", "100%",
            "height", "100%",
            "align", "middle",
            "id", "accueil",
            "quality", "high",
            "bgcolor", "#869ca7",
            "name", "accueil",
            "flashvars",'historyUrl=history.htm%3F&lconid=' + lc_id + '&SessionLang=<cfoutput>#session.lang#</cfoutput>',
            "allowScriptAccess","sameDomain",
            "type", "application/x-shockwave-flash",
            "pluginspage", " http://www.adobe.com/go/getflashplayer"
            );
            } else { // flash is too old or we can't detect the plugin
            var alternateContent = 'Alternate HTML content should be placed here. '
            + 'This content requires the Adobe Flash Player. '
            + '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
            document.write(alternateContent); // insert non-flash content
            }
            // -->
            </script>
            <noscript>
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
            id="accueil" width="100%" height="100%"
            codebase=" http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
            <param name="movie" value="accueil.swf?SessionLang=<cfoutput>#session.lang#</cfoutput>" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#869ca7" />
            <param name="allowScriptAccess" value="sameDomain" />
            <embed src="accueil.swf?SessionLang=<cfoutput>#session.lang#</cfoutput>" quality="high" bgcolor="#869ca7"
            width="100%" height="100%" name="accueil" align="middle"
            play="true"
            loop="false"
            allowScriptAccess="sameDomain"
            type="application/x-shockwave-flash"
            pluginspage=" http://www.adobe.com/go/getflashplayer">
            </embed>
            </object>
            </noscript>

            <iframe name="_history" src="history.htm" frameborder="0" scrolling="no" width="22" height="0"></iframe>
            </body>
            </html>

            • 3. Re: Passing Parameter(s) to the fLex SWF Compiled Object
              francois-yanick Level 1
              And here is my source code for my SWF file:

              Accueil.swf

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              layout="horizontal"
              backgroundGradientColors="[#FFFFFF, #B5DEFF]"
              horizontalAlign="center" verticalAlign="top"
              xmlns:MyComp="*"
              horizontalScrollPolicy="on" verticalScrollPolicy="on"
              initialize="getInit()"
              pageTitle="Mush Hi-Tech"
              >

              <mx:Script>
              <![CDATA[
              import mx.controls.Button;

              var SessionLang:String = Application.application.parameters.SessionLang;

              public function getInit():void{
              if (SessionLang == "fr") {
              ApplicationControlBarLabel1.text = "Mush Hi-Tech - Spécialiste du gangline";
              navLangFR.alpha = 0.3;
              Panel1.title = "Nos Produits";
              Panel2.title = "Nos Services";
              Panel3.title = "Détails";
              } else {
              ApplicationControlBarLabel1.text = "Mush Hi-Tech - Specialist of gangline";
              navLangEN.alpha = 0.3;
              Panel1.title = "Our Products";
              Panel2.title = "Our Services";
              Panel3.title = "Details";
              }
              }

              ]]>
              </mx:Script>

              <mx:ApplicationControlBar dock="true">
              <mx:Label
              id="ApplicationControlBarLabel1"
              text="Mush Hi-Tech - Spécialiste du gangline"
              fontFamily="Verdana" fontWeight="bold" fontSize="12" color="white"
              />
              <mx:Spacer width="100%"/>
              <mx:Button id="navLangFR"
              label="Français"
              color="#000000"
              fontSize="10"
              click="navigateToURL(new URLRequest(' http://www.mushhitech.com/v2007/lang.cfm?l=1'), '_self')"
              alpha="1.0"
              />
              <mx:Button id="navLangEN"
              label="English"
              color="#000000"
              fontSize="10"
              click="navigateToURL(new URLRequest(' http://www.mushhitech.com/v2007/lang.cfm?l=2'), '_self')"
              alpha="1.0"
              />
              </mx:ApplicationControlBar>

              <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="800">
              <mx:VBox>
              <mx:HBox width="100%">
              <MyComp:header getLanguage="{SessionLang}"/>
              </mx:HBox>
              <mx:HBox width="100%">
              <mx:VBox width="70%">
              <mx:Panel id="Panel1" width="100%" title="Nos Produits"
              backgroundColor="white"
              height="300"
              cornerRadius="10">
              <MyComp:home_panel1 getLanguage="{SessionLang}"/>
              </mx:Panel>
              <mx:Panel id="Panel2" width="100%" title="Nos Services"
              backgroundColor="white"
              height="100"
              cornerRadius="10">

              </mx:Panel>
              </mx:VBox>
              <mx:Panel id="Panel3" width="30%" title="Details"
              backgroundColor="white"
              height="407"
              cornerRadius="10">
              </mx:Panel>
              </mx:HBox>
              <MyComp:footer getLanguage="{SessionLang}"/>
              </mx:VBox>
              </mx:Canvas>

              </mx:Application>
              • 4. Re: Passing Parameter(s) to the fLex SWF Compiled Object
                francois-yanick Level 1
                ERRATUM

                Actually, I was wrong with my latest statement by saying that the new code was not generating any errors as I forgot to upload the SWF recompiled.

                Now that it is uploaded, the system return me this error:

                TypeError: Error #1009: Cannot access a property or method of a null object reference.
                at accueil$iinit()
                at mx.managers::SystemManager/create()
                at mx.managers::SystemManager/::initializeTopLevelWindow()
                at mx.managers::SystemManager/::frameEndHandler()
                • 5. Re: Passing Parameter(s) to the fLex SWF Compiled Object
                  francois-yanick Level 1
                  According to the message from Matlus, the new "sentance" Application.application.parameters.[parameter-name] was a start for a new search and, according to this page: http://livedocs.macromedia.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=L iveDocs_Parts&file=00001004.html, I found that my variable has to be declare publically.

                  I modified the source file and it is now working well.

                  Here is my new source for the Flex SWF file:

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                  layout="horizontal"
                  backgroundGradientColors="[#FFFFFF, #B5DEFF]"
                  horizontalAlign="center" verticalAlign="top"
                  xmlns:MyComp="*"
                  horizontalScrollPolicy="on" verticalScrollPolicy="on"
                  initialize="getInit()"
                  pageTitle="Mush Hi-Tech"
                  >

                  <mx:Script>
                  <![CDATA[
                  import mx.controls.Button;

                  [Bindable]
                  public var SessionLang:String;

                  public function getInit():void{
                  SessionLang = Application.application.parameters.SessionLang;
                  if (SessionLang == "fr") {
                  ApplicationControlBarLabel1.text = "Mush Hi-Tech - Spécialiste du gangline";
                  navLangFR.alpha = 0.3;
                  Panel1.title = "Nos Produits";
                  Panel2.title = "Nos Services";
                  Panel3.title = "Détails";
                  } else {
                  ApplicationControlBarLabel1.text = "Mush Hi-Tech - Specialist of gangline";
                  navLangEN.alpha = 0.3;
                  Panel1.title = "Our Products";
                  Panel2.title = "Our Services";
                  Panel3.title = "Details";
                  }
                  }

                  ]]>
                  </mx:Script>

                  <mx:ApplicationControlBar dock="true">
                  <mx:Label
                  id="ApplicationControlBarLabel1"
                  text="Mush Hi-Tech - Spécialiste du gangline"
                  fontFamily="Verdana" fontWeight="bold" fontSize="12" color="white"
                  />
                  <mx:Spacer width="100%"/>
                  <mx:Button id="navLangFR"
                  label="Français"
                  color="#000000"
                  fontSize="10"
                  click="navigateToURL(new URLRequest(' http://www.mushhitech.com/v2007/lang.cfm?l=1'), '_self')"
                  alpha="1.0"
                  />
                  <mx:Button id="navLangEN"
                  label="English"
                  color="#000000"
                  fontSize="10"
                  click="navigateToURL(new URLRequest(' http://www.mushhitech.com/v2007/lang.cfm?l=2'), '_self')"
                  alpha="1.0"
                  />
                  </mx:ApplicationControlBar>

                  <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="800">
                  <mx:VBox>
                  <mx:HBox width="100%">
                  <MyComp:header getLanguage="{SessionLang}"/>
                  </mx:HBox>
                  <mx:HBox width="100%">
                  <mx:VBox width="70%">
                  <mx:Panel id="Panel1" width="100%" title="Nos Produits"
                  backgroundColor="white"
                  height="300"
                  cornerRadius="10">
                  <MyComp:home_panel1 getLanguage="{SessionLang}"/>
                  </mx:Panel>
                  <mx:Panel id="Panel2" width="100%" title="Nos Services"
                  backgroundColor="white"
                  height="100"
                  cornerRadius="10">

                  </mx:Panel>
                  </mx:VBox>
                  <mx:Panel id="Panel3" width="30%" title="Details"
                  backgroundColor="white"
                  height="407"
                  cornerRadius="10">
                  </mx:Panel>
                  </mx:HBox>
                  <MyComp:footer getLanguage="{SessionLang}"/>
                  </mx:VBox>
                  </mx:Canvas>

                  </mx:Application>
                  • 6. Re: Passing Parameter(s) to the fLex SWF Compiled Object
                    Matlus Level 1
                    François,

                    I'm not sure what you're saying needs to be public...

                    the "parameter_name" is a dynamic property that is determined by you simply by having a flashvar named as such. You should be able to assign it to any scoped variable (public or private).

                    In fact in my case I I assign the value to a local (to a method) variable.

                    Anyway, at least its working!
                    • 7. Passing Parameter(s) to the fLex SWF Compiled Object
                      francois-yanick Level 1
                      You are totally right...

                      The Applicatiom.application.parameters.[name of the variale from the flashvars] can be seen everywhere as it is include in the Application scope.

                      Thank you very much for your help!