2 Replies Latest reply on Dec 7, 2007 11:48 AM by CFuffle

    Changing states with actionScript

    CFuffle
      Hi,

      I have some states set up in my app using MXML as per the script sample below. This is of course very handy, but upon changing from one state to another I want to include some actionScript to make a few things happen when the state changes. I know that I have full control over states in AS but can I set up the states as below using MXML and then use AS just to perform the change. Currently, selecting a new item in a dataGrid calls the change (change="currentState='editProperty'") but I'd prefer to have this (change="editProperty()") which would call an AS function to do some work and *then* change the state. Any help appreciated as usual,
      <!-- States -->
      <mx:states>
      <!-- Edit property state (multipage form)-->
      <mx:State name="editProperty">
      <mx:SetProperty target="{props}" name="height" value="262"/>
      <mx:SetProperty target="{props}" name="x"/>
      <mx:AddChild position="lastChild">
      <mx:VBox width="100%" height="100%">
      -- various controls etc. here --
      </mx:VBox>
      </mx:AddChild>
      </mx:State>
      </mx:states>

      cheers,

      Mike
        • 1. Re: Changing states with actionScript
          dsimpson_cs3 Level 1
          You could write an actionscript method which takes any class that extends UIComponent. This includes Application, Canvas, Panel, etc...

          All have a current state

          I actually have some code which does just that

          package com.domain.ecommerce.admin
          {

          import mx.events.ItemClickEvent;
          import mx.controls.Alert;
          import mx.core.UIComponent;

          public class StateManager
          {

          /**
          * changes the current state based on anything that will generate an ItemClickEvent...
          *
          * For exmaple: <mx:LinkBar itemClick="StateManager.chooseState(targetComponent,event)" > ... </mx:LinkBar>
          * note: this is the object the link bar is located in ...
          */
          public static function chooseState(component:UIComponent, event:ItemClickEvent) : void {

          component.currentState = event.label;
          }


          public static function chooseStateByString(component:UIComponent, newState:String) : void {
          component.currentState = newState;
          }

          }
          }



          You can do something like :

          click="StateManager.chooseStateByString(this, 'newState');"
          • 2. Re: Changing states with actionScript
            CFuffle Level 1
            Thanks dsimpson_cs3,

            you've made an old procedural progammer very happy! I'll try to get to grips with that code tomorrow but it looks like it's just what I need,

            cheers,

            Mike