9 Replies Latest reply on Sep 8, 2009 3:20 PM by Seglespaan

    states

    Seglespaan Level 1

      How do u get the current state of say the login form demo?

       

      so i button clicked and current state is register do this else do this?

       

      I've tried currentstate property but to no avail?

       

      Cheers

       

      Tim

        • 1. Re: states
          run,ryan! Level 3

          selectedIndex

          • 2. Re: states
            Seglespaan Level 1

            So in this case how would i use the selected index to trace the forms state when the button is pressed?

             

            <?xml version="1.0" ?>
            <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
                height="324" borderColor="#C0C0C0" width="341"
                backgroundColor="#FFFFFF" cornerRadius="20"
                creationComplete="init()">

             

            <mx:Script>
                <![CDATA[
                    public function init()
                       {
                          loginButton.addEventListener(MouseEvent.CLICK, login_handler);   
                       }
                      
                      
                    public function login_handler(e:MouseEvent)
                       {
                              trace(loginForm.selectedIndex);
                       } 

             

                      
                ]]>
            </mx:Script>

             

                <!-- Define one view state, in addition to the base state.-->
                <mx:states>
                    <mx:State name="Register">
                        <mx:AddChild relativeTo="{loginForm}" position="lastChild">
                            <mx:target>
                                <mx:FormItem id="email" label="Email:">
                                    <mx:TextInput/>
                                </mx:FormItem>
                            </mx:target>
                        </mx:AddChild>
                        <mx:SetProperty target="{loginPanel}" name="title" value="Register"/>
                        <mx:SetProperty target="{loginButton}" name="label" value="Register"/>
                        <mx:SetStyle target="{loginButton}"
                            name="color" value="blue"/>
                        <mx:RemoveChild target="{registerLink}"/>
                        <mx:AddChild relativeTo="{spacer1}" position="before">
                            <mx:target>
                                <mx:LinkButton id="loginLink" label="Return to Login" click="currentState=''"/>
                            </mx:target>
                        </mx:AddChild>
                        <mx:SetProperty target="{loginPanel}" name="height" value="272"/>
                    </mx:State>
                </mx:states>

             

                <mx:transitions>
                    <!-- Define the transition from the base state to the Register state.-->
                    <mx:Transition id="toRegister" fromState="*" toState="Register">
                        <mx:Sequence targets="{[loginPanel, registerLink, email, loginLink, spacer1]}">
                            <mx:RemoveChildAction/>
                            <mx:SetPropertyAction target="{loginPanel}" name="title"/>
                            <mx:SetPropertyAction target="{loginButton}" name="label"/>
                            <mx:SetStyleAction target="{loginButton}" name="color"/>
                            <mx:Resize target="{loginPanel}"/>
                            <mx:AddChildAction/>
                        </mx:Sequence>
                    </mx:Transition>

             

                    <!-- Define the transition from the Register state to the base state.-->
                    <mx:Transition id="toDefault" fromState="Register" toState="*">
                        <mx:Sequence targets="{[loginPanel, registerLink,
                                email, loginLink, spacer1]}">
                            <mx:RemoveChildAction/>
                            <mx:SetPropertyAction target="{loginPanel}" name="title"/>
                            <mx:SetPropertyAction  target="{loginButton}" name="label"/>
                            <mx:SetStyleAction target="{loginButton}" name="color"/>
                            <mx:Resize target="{loginPanel}"/>
                            <mx:AddChildAction/>
                        </mx:Sequence>
                    </mx:Transition>
                </mx:transitions>

             

                <!-- Define a Panel container that defines the login form.-->
                <mx:Panel title="Login" id="loginPanel"
                    horizontalScrollPolicy="off" verticalScrollPolicy="off"
                    paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
               
                    <mx:Text width="100%" color="#000000"
                        text="Enter your username and password to login, alternatively click register to create a new account.;"/>

             

                    <mx:Form id="loginForm" >
                        <mx:FormItem label="Username:">
                            <mx:TextInput/>
                        </mx:FormItem>
                        <mx:FormItem label="Password:">
                            <mx:TextInput/>
                        </mx:FormItem>
                    </mx:Form>
                    <mx:ControlBar>
                        <mx:LinkButton id="registerLink"  label="Need to Register?"
                            click="currentState='Register'"/>
                        <mx:Spacer width="100%" id="spacer1"/>
                        <mx:Button label="Login" id="loginButton"/>
                    </mx:ControlBar>
                </mx:Panel>
            </mx:TitleWindow>

            • 3. Re: states
              Andrew Rosewarn Level 3

              Hi

               

              To find the current state that is displayed you can use the currentState statement its read write.

               

              trace(currentState);

               

              Andrew

              • 4. Re: states
                Seglespaan Level 1

                I've tried that but it echo's null??

                • 5. Re: states
                  Andrew Rosewarn Level 3

                  This was my test

                   

                  <?xml version="1.0"?>

                   

                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initCollections();" >

                      <mx:states>

                          <mx:State name="state2">

                              <mx:RemoveChild target="{label1}"/>

                              <mx:AddChild position="lastChild">

                                  <mx:Label text="THIS IS STATE 2"/>

                              </mx:AddChild>

                          </mx:State>

                      </mx:states>

                   

                      <mx:Script>

                          <![CDATA[

                   

                              import mx.events.MenuEvent;

                              import mx.controls.Alert;

                              import mx.collections.*;

                   

                              [Bindable]

                              public var menuBarCollection:XMLListCollection;

                   

                              private var menubarXML:XMLList =

                                  <>

                                      <menuitem label="Menu1" data="top">

                                          <menuitem label="First State" data=""/>

                                          <menuitem label="Second State" data="state2"/>

                                      </menuitem>

                   

                                  </>;

                   

                            private function initCollections():void {

                                  menuBarCollection = new XMLListCollection(menubarXML);

                              }

                   

                              private function menuHandler(event:MenuEvent):void  {

                                  currentState = event.item.@data;

                              }

                           ]]>

                      </mx:Script>

                   

                  <mx:MenuBar labelField="@label" itemClick="menuHandler(event);"

                          dataProvider="{menuBarCollection}" />

                       <mx:Label text="THIS IS STATE 1 - DEFAULT" id="label1"/>

                       <mx:Button label="Show state" click="{mx.controls.Alert.show(currentState)}"/>

                  </mx:Application>

                   

                  Andrew

                  • 6. Re: states
                    Seglespaan Level 1

                    any idea how I can adapt my example to trace the current state?

                    • 7. Re: states
                      Andrew Rosewarn Level 3

                      This works for me

                       

                      <?xml version="1.0" ?>

                      <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"

                          height="324" borderColor="#C0C0C0" width="341"

                          backgroundColor="#FFFFFF" cornerRadius="20"

                          creationComplete="init()">

                       

                      <mx:Script>

                          <![CDATA[

                          import mx.controls.Alert;

                              public function init():void

                                 {

                                    loginButton.addEventListener(MouseEvent.CLICK, login_handler);   

                                 }

                       

                       

                              public function login_handler(e:MouseEvent):void

                                 {

                                        mx.controls.Alert.show(currentState);

                                 }

                       

                       

                          ]]>

                      </mx:Script>

                       

                          <!-- Define one view state, in addition to the base state.-->

                          <mx:states>

                              <mx:State name="Register">

                                  <mx:AddChild relativeTo="{loginForm}" position="lastChild">

                                      <mx:target>

                                          <mx:FormItem id="email" label="Email:">

                                              <mx:TextInput/>

                                          </mx:FormItem>

                                      </mx:target>

                                  </mx:AddChild>

                                  <mx:SetProperty target="{loginPanel}" name="title" value="Register"/>

                                  <mx:SetProperty target="{loginButton}" name="label" value="Register"/>

                                  <mx:SetStyle target="{loginButton}"

                                      name="color" value="blue"/>

                                  <mx:RemoveChild target="{registerLink}"/>

                                  <mx:AddChild relativeTo="{spacer1}" position="before">

                                      <mx:target>

                                          <mx:LinkButton id="loginLink" label="Return to Login" click="currentState=''"/>

                                      </mx:target>

                                  </mx:AddChild>

                                  <mx:SetProperty target="{loginPanel}" name="height" value="272"/>

                              </mx:State>

                          </mx:states>

                       

                          <mx:transitions>

                              <!-- Define the transition from the base state to the Register state.-->

                              <mx:Transition id="toRegister" fromState="*" toState="Register">

                                  <mx:Sequence targets="{[loginPanel, registerLink, email, loginLink, spacer1]}">

                                      <mx:RemoveChildAction/>

                                      <mx:SetPropertyAction target="{loginPanel}" name="title"/>

                                      <mx:SetPropertyAction target="{loginButton}" name="label"/>

                                      <mx:SetStyleAction target="{loginButton}" name="color"/>

                                      <mx:Resize target="{loginPanel}"/>

                                      <mx:AddChildAction/>

                                  </mx:Sequence>

                              </mx:Transition>

                       

                              <!-- Define the transition from the Register state to the base state.-->

                              <mx:Transition id="toDefault" fromState="Register" toState="*">

                                  <mx:Sequence targets="{[loginPanel, registerLink,

                                          email, loginLink, spacer1]}">

                                      <mx:RemoveChildAction/>

                                      <mx:SetPropertyAction target="{loginPanel}" name="title"/>

                                      <mx:SetPropertyAction  target="{loginButton}" name="label"/>

                                      <mx:SetStyleAction target="{loginButton}" name="color"/>

                                      <mx:Resize target="{loginPanel}"/>

                                      <mx:AddChildAction/>

                                  </mx:Sequence>

                              </mx:Transition>

                          </mx:transitions>

                       

                          <!-- Define a Panel container that defines the login form.-->

                          <mx:Panel title="Login" id="loginPanel"

                              horizontalScrollPolicy="off" verticalScrollPolicy="off"

                              paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

                       

                              <mx:Text width="100%" color="#000000"

                                  text="Enter your username and password to login, alternatively click register to create a new account.;"/>

                       

                              <mx:Form id="loginForm" >

                                  <mx:FormItem label="Username:">

                                      <mx:TextInput/>

                                  </mx:FormItem>

                                  <mx:FormItem label="Password:">

                                      <mx:TextInput/>

                                  </mx:FormItem>

                              </mx:Form>

                              <mx:ControlBar>

                                  <mx:LinkButton id="registerLink"  label="Need to Register?"

                                      click="currentState='Register'"/>

                                  <mx:Spacer width="100%" id="spacer1"/>

                                  <mx:Button label="Login" id="loginButton"/>

                              </mx:ControlBar>

                          </mx:Panel>

                      </mx:TitleWindow>

                       

                       

                      If you click login in the baseState then its gonna show blank, cos the default state is "".  When in register it works.

                       

                      I've removed the loginComp.currentState and just got currentState cos that refers to 'this', the comp its in

                       

                      Does that help

                       

                      Andrew

                      • 8. Re: states
                        Seglespaan Level 1

                        perfect, thanks for your help!

                         

                        Just one more thing tho, when I open the register part then trace it shows register, should it not then trace login when I switch back to the base state?

                        • 9. Re: states
                          Andrew Rosewarn Level 3

                          No because you do not have a state called login.  The base state is referred to as "".

                           

                          ie to set the state to register you do

                           

                          currentState = 'Register'

                           

                          and to set it back to the base state

                           

                          currentState = ''

                           

                          Thats the way it works

                           

                          Andrew