2

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.

ketan
  • 19,129
  • 42
  • 60
  • 98
Maltiriel
  • 793
  • 2
  • 11
  • 28
  • Yes, that's what they're supposed to do, but they were intended to be used in MXML notation. Technically you can use states in ActionScript but as you've pointed out, that's kind of convoluted, as that is not what they were intended for. Perhaps you should be more concrete about what you're trying to achieve. Maybe we can show you how it is done without using ActionScript code. – RIAstar Mar 30 '12 at 23:16
  • @RIAstar I don't think there'd be a good way to do this completely without ActionScript... There are a bunch of different components that change in different ways (icons, enabled/disabled, etc.) based on the current state group. ETA Can you explain more about why state groups/states aren't supposed to be used in ActionScript? I don't really understand this. – Maltiriel Apr 02 '12 at 14:45

1 Answers1

1

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>
Sunil D.
  • 17,983
  • 6
  • 53
  • 65