1 Reply Latest reply on Oct 24, 2012 2:36 AM by Gaius Coffey

    Can I call a web app and load a specific state (not the defeult state) within the url?

    cfortin14 Level 1

      I have a web app with many states, can I load the app and activate a specific state (not the default) from the url?

       

       

      Thank you in advanced,

       

       

      Carlos

        • 1. Re: Can I call a web app and load a specific state (not the defeult state) within the url?
          Gaius Coffey Level 2

          Yes, but you need to implement it yourself.

           

          There are two methods.

          1. You can set a flashvars parameter in your HTML launch code for Flash. This will be available in the parameters property of your application.

          2. You can implement a BrowserManager listener to read the url fragment.

           

          For example, you could do something like this:

          [code]

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                xmlns:s="library://ns.adobe.com/flex/spark"

                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

                creationComplete="application1_creationCompleteHandler(event)">

           

          <fx:Script>

            <![CDATA[

             import mx.events.BrowserChangeEvent;

             import mx.events.FlexEvent;

             import mx.managers.BrowserManager;

             import mx.managers.IBrowserManager;

             // Set a constant for arg name to prevent typos!

             public const SELECT_STATE_ARG:String = "selectState";

             /**

              * Initialise app to use either flashvars parameters or BrowserManager to set state.

              * Will respond to selectState value in either fragment or flashvars.

              */

             protected function application1_creationCompleteHandler(event:FlexEvent):void

             {

              // Method 1: This tries to load based on value of selectState in flashvars in your launch code.

              if(parameters && parameters[SELECT_STATE_ARG]) {

               tryState(parameters[SELECT_STATE_ARG].toString());

              }

             

              // Method 2: Set up browser manager to listen.

              // 1. Get instance

              var bm:IBrowserManager = BrowserManager.getInstance();

              // 2. Init to get current url

              bm.init();

              // 3. Add event listener for hash tag changes

              bm.addEventListener(BrowserChangeEvent.URL_CHANGE,processBrowserChange);

              // 4. Call listener once to set initial state.

              processBrowserChange(null);

             }

             /**

              * Try going to the specified state or go to errored state with details if it fails.

              */

             protected function tryState(tryLoadState:String):void {

              try {

               currentState = tryLoadState;

              } catch(e:Error) {

               errorDetails  = "State not found: "+tryLoadState+"\n"+e.toString();

               currentState = "errored";

              }

             }

             [Bindable] protected var errorDetails:String;

             /**

              * Look for a fragment and search for selectState - try loading that state if found.

              */

             protected function processBrowserChange(event:BrowserChangeEvent):void {

              var tryLoadState:String;

              var bm:IBrowserManager = BrowserManager.getInstance();

              var fragment:String = bm.fragment;

              if(fragment) {

               try {

                var uv:URLVariables = new URLVariables(fragment);

                tryLoadState = uv[SELECT_STATE_ARG];

               } catch(e:Error) {

                errorDetails  = "Could not parse: "+fragment+" as URLVariables\n"+e.toString();

                currentState = "errored";

                return;

               }

               if(tryLoadState) tryState(tryLoadState);

              }   

             }

            

            ]]>

          </fx:Script>

          <fx:Declarations>

            <!-- Place non-visual elements (e.g., services, value objects) here -->

          </fx:Declarations>

          <s:states>

            <s:State name="aState" />

            <s:State name="aNotherState" />

            <s:State name="yetANotherState" />

            <s:State name="errored" />

          </s:states>

          <s:VGroup horizontalCenter="0" verticalCenter="0">

            <s:Label text="currentState is {currentState}" />

            <s:Label text="{errorDetails}" color="#ff0000" includeIn="errored" />

          </s:VGroup>

          </s:Application>

          [/code]

           

          That will allow you to change the state of your app on demand by linking to, for example:

          http://<myUrl>/myapp.html#selectState=aNotherState

           

          Cheers,

          G