Calendar

May 2009
S M T W T F S
« Apr   Jun »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tag Cloud

Categories

Archives

Recent Posts

Recent Comments


« 360|Flex Teaser – Using LocalConnection | Main | InsideRIA the Conference! »

Flex 3 vs Flex 4 State Management

By Rich Tretola | May 12, 2009
11,008 views

Here is a very simple example of how State management has changed between Flex 3 and Flex 4. Other than the default skin, both of these applications are identical in their resulting swfs. However, how they are coded is much different. Take a look at the code and samples below (Note: Click on the buttons!).

Flex 3 States Example

Get Adobe Flash player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" width="400" height="300">
   
    <mx:states>
        <mx:State name="State2">
            <mx:SetProperty target="{button1}"
                name="label" value="Big"/>
            <mx:SetProperty target="{button1}"
                name="width" value="200"/>
            <mx:SetProperty target="{button1}"
                name="height" value="200"/>
            <mx:SetEventHandler target="{button1}"
                name="click" handler="currentState=''"/>
            <mx:AddChild relativeTo="{vbox1}"
                position="lastChild">
                <mx:Label text="State 2 Only"
                    width="100%"
                    textAlign="center"/>
            </mx:AddChild>
        </mx:State>
    </mx:states>
   
    <mx:VBox verticalCenter="0"
            horizontalCenter="0"  id="vbox1">
        <mx:Button label="Small"
            click="currentState='State2'"
            id="button1"/>     
    </mx:VBox>

</mx:Application>

Flex 4 States Example

Get Adobe Flash player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<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/halo"
    width="400" height="300">
   
    <s:states>
        <s:State name="State1"/>
        <s:State name="State2"/>
    </s:states>

    <mx:VBox verticalCenter="0"
        horizontalCenter="0" >
   
        <s:Button label.State1="Small"
            width.State2="200"
            height.State2="200"
            label.State2="BIG"
            click.State1="currentState='State2'"
            click.State2="currentState='State1'"/>
           
        <mx:Label includeIn="State2"
            width="100%"
            text="State 2 Only"
            textAlign="center"/>
           
    </mx:VBox>
   
</s:Application>

Flex 3 handles the changes to my buttons properties through a series of mx:SetProperty tags as well as a mx:SetEventHandler tag within the mx:states declaration.

While Flex 4 uses a simpler dot notation style declaration within the original Button component. This is certainly easier for developers working outside of the design view within Flex Builder.

Also, notice how the mx:Label that only exists within State2 in each application is handled. Within the Flex 3 application, this is accomplished with the mx:AddChild tag within the mx:states declaration, while in the Flex 4 application it is as simple as having a comma seperated list of states to include the component in within the Labels includeIn property.

These changes are not something that an end user would ever know or care about however as developers these types of refactoring changes to the tools and SDK are much appreciated.

Thanks Adobe!

Topics: Flex, Flex 3 (Moxie), Flex 4 | 16 Comments »

16 Responses to “Flex 3 vs Flex 4 State Management”

  1. lekant Says:
    May 12th, 2009 at 10:27 am

    I like it !

    Reply to this comment

  2. fabianv Says:
    May 12th, 2009 at 11:04 am

    Yeah, this is def an improvement from the old way of doing states. It just feels more natural.

    I’m curious about transitions though, they seem to be still in the same kind of style as the old states.. or am I mistaken?

    Nice article :)

    Reply to this comment

  3. E Brent Nelson Says:
    May 12th, 2009 at 1:34 pm

    This is going to be SO much better. I dislike states because of how they are currently implemented. I will definitely appreciated them more in the future!

    Reply to this comment

  4. Justin J. Moses Says:
    May 12th, 2009 at 8:15 pm

    Nice overview Rich.

    I wonder if this new take on states will convince developers who traditionally use viewstacks within their MVC frameworks to switch…

    justin

    Reply to this comment

    Rich Tretola Reply:

    Not sure, I know it will change my development habits.

    Reply to this comment

  5. batchass Says:
    May 13th, 2009 at 1:23 am

    that’s good!

    Is there a way to use flex4 to create an air app ?
    Just a switch of the sdk in Flex Builder?
    Thanks

    Reply to this comment

  6. polyGeek Says:
    May 13th, 2009 at 2:35 am

    I’m very excited. I haven’t used states in a Flex app in I don’t know how long – years. This just makes since.

    Go Flex-team go!

    Reply to this comment

  7. Jodie O'Rourke Says:
    May 13th, 2009 at 5:11 am

    It was great to see a demo of this by Andrew Shorten at the Midlands FPUG, but I don’t think will change the way I built Flex apps.

    Reply to this comment

  8. Jamie Says:
    May 14th, 2009 at 2:01 pm

    Oooo, I like this a lot!! The Flex3 and under method is extremely hard to read, looking at the code.

    Reply to this comment

  9. Flash Builder 4 is Public | EverythingFlex: Flex & AIR Says:
    June 1st, 2009 at 9:22 am

    [...] Flash Builder 4 also comes with the new Flex 4 SDK. Check out how State management has changed in Flex 4. [...]

  10. John Says:
    June 29th, 2009 at 1:27 pm

    Here i found another good article explaining use of states for creating a website using states in flex .would like to share
    here is link :
    http://askmeflash.com/tutorial_m.php?p=tutorial&tid=0

    Reply to this comment

  11. Edward Mansouri Says:
    July 28th, 2009 at 9:20 am

    Yeah this is a nice improvement overall however I have an app I am migrating for Flex 4 now that has 39 different individual states defined with so I think it will take me a bit of time to get excited about this!

    Reply to this comment

  12. ActionScriptJobs.com Says:
    August 19th, 2009 at 2:23 pm

    [...] Flex 3 vs Flex 4 State Management [...]

  13. Flex 4 Resources – Samples and Links! « Devgirl’s Weblog Says:
    September 4th, 2009 at 6:22 pm

    [...] Flex 3 versus Flex 4 State Management [...]

  14. MBT shoes store Says:
    December 30th, 2009 at 8:52 pm

    Mbt shoes are so amazing. You will have a brand new walk experience when putting on Mbt m walk, Mbt Chapa and the like

    Reply to this comment

  15. 外汇交易 Says:
    February 2nd, 2010 at 6:08 am

    Mbt shoes are so amazing. You will have a brand new walk experience when putting on Mbt m walk, Mbt Chapa and the like

    Reply to this comment

Comments