3 Replies Latest reply on Jan 15, 2013 5:29 PM by Captain825

    Multiple States v. Viewstack

    Captain825

      Hi

       

      Kinda new to programming in Flex 4.6 but I would appreciate some comments on my line of thinking.

       

      I am developing an application that has a menu at the top of a view state with 10 different choices.  When a user clicks on a choice, I change to the appropriate State that holds the appropriate form.  The same menu is also displayed at the top of the new State.  I took a look at the code and realized that it created an enormous amount of duplicate code to have ten States with ten of the same menus at the top which seems very inefficient to me.

       

      I am now thinking I should have a single State with a ViewStack with each viewstack container being a seperate form depending on which button the user clicks on the menu.

       

      Keeping in mind that I am relatively new, would using viewstacks in this situation be the most efficient?

       

      Thanks in advance.

        • 1. Re: Multiple States v. Viewstack
          jfb00 Level 3

          Hi,

          I was having the same issue long time ago until i discover modules.

          Please take a look at this:

          http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-799a.html

          http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf64277-7ffd.html

          It is not an easy subject but try and see if you can use modules.

          I hope this help.

          1 person found this helpful
          • 2. Re: Multiple States v. Viewstack
            jfb00 Level 3

            I created a simple example that may help you and others to understand.

             

            <?xml version="1.0"?>

            <!-- modules/StyleModLoaderApp.mxml -->

            <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"

                creationComplete="init()">

               

                <s:layout>

                    <s:VerticalLayout/>

                </s:layout>

               

                <fx:Script>

                    <![CDATA[

                       

                        private function init():void{

                            myMenuBar.addEventListener(MouseEvent.CLICK, openMenuItem_click);

                        }

                       

                        protected function openMenuItem_click(evt:MouseEvent):void {

                            var tempIndex:Object  = evt.currentTarget;

                            if (tempIndex.selectedIndex != -1){

                                switch( tempIndex.selectedIndex ){

                                    case 1:

                                        myModule.url = "moduleA.swf";

                                        break;

                                    case 2:

                                        myModule.url = "moduleB.swf"

                                        break;

                                    default:

                                        break;

                                }

                            }

                        }

                    ]]>

                </fx:Script>

               

                <mx:MenuBar id="myMenuBar" labelField="@label" >

                    <fx:XMLList>

                        <menuitem label="MenuItem A">

                            <menuitem label="SubMenuItem A-1" enabled="false"/>

                            <menuitem label="SubMenuItem A-2"/>

                        </menuitem>

                        <menuitem label="Module A"/>

                        <menuitem label="Module B"/>

                       

                    </fx:XMLList>

                </mx:MenuBar>

                <s:ModuleLoader id="myModule" />

               

            </s:Application>

            • 3. Re: Multiple States v. Viewstack
              Captain825 Level 1

              Wow, thank you very much.  You've gone above and beyond and I will digest your example and I'm sure I will be able to understand the concepts.

               

              Thank you very much