Search code examples
actionscript-3apache-flexviewstateflex4.5

Is there an easy way to get the state group or the actual State object for the current state?


I'm using Flex 4.5 and trying to take advantage of the new state groups feature. I have two States (call them readType1 and readType2) that both belong to the same stateGroup (call it readOnly). There are several places where I'd like to do something based on the current state, and it would be the same thing for the two read states. E.g.:

if (this.currentState == "readType1" || this.currentState == "readType2")
{
  //do stuff
}

What I'm wondering is if there's a way to change this so that it's based on the stateGroup (because logically that's really what it's based on, not on the actual exact states) in an easy way. I would like to do something like:

if (this.currentStateGroup == "readOnly")
{
  //do stuff
}

or failing that

if (this.currentStateObject.stateGroups.indexOf("readOnly") >= 0)
{
  //do stuff
}

I haven't been able to find a way to get the name of the current stateGroup, nor to get ahold of the actual State object for the current state (currentState is just a String) without looping through this.states. I'm hoping I'm missing something and there's a way to get this easily...

I thought this was the sort of thing state groups were supposed to help with: grouping common behaviors together. Or am I misunderstanding the point of state groups? There aren't many examples or much info available on them, and the examples I have found haven't been good enough to really understand how one would use them in a real app.

Anyhow if anyone knows of a way to do what I want I'd appreciate it, or if I'm thinking about this all wrong, please let me know. Thanks.


Solution

  • Perhaps you can turn this problem on it's ear:

    Do your work when entering the appropriate states. In MXML use an "enterState" listener on each state that belongs to the stateGroup:

    <s:states>
      <s:State name="readType1" enterState="onEnterReadState()" stateGroups="readGroup"/>
    </s:states>