Search code examples
actionscript-3apache-flexflex4flash-builderviewstate

Flex4 Application state in a custom component


I'm porting my Flex3 app to Flex4 (FlashBuilder4). I get the whole new state concept, except one thing. In a custom component (separate mxml file) I'm using the main level Application's state. In Flex3 it was:

<mx:State name="only_view_mode">
    <mx:RemoveChild target="{myComponent.button1}" />
</mx:State>

In Flex4 it should be something like that:

<mx:State name="only_view_mode" />

and

<mx:LinkButton id="button1" excludeFrom="???" />

My question is: how can I access to an Application state from a component? I checked the official reference (http://www.adobe.com/go/learn_flex4_alldocumentation_en) and Google of course but without any success.

Thanks in advance


Solution

  • That didn't really work since it requires a String. I tried it in every possible way I could figure out. So far I came with this very ugly solution: I add an event listener for checkint state changes of my main level component:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx" 
             creationComplete="{onCreationComplete();}">
        <fx:Script>
            <![CDATA[
                import mx.core.FlexGlobals;
                import mx.events.StateChangeEvent;
                private function onCreationComplete():void {
                    FlexGlobals.topLevelApplication.addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE, onStateChanged);
                }
    
                private function onStateChanged(event:StateChangeEvent):void {
                    currentState = event.newState;
                }
            ]]>
        </fx:Script>
        <s:states>
            <s:State name="default" />
            <s:State name="login" />
        </s:states>
        <s:TextArea includeIn="login"/>
    </s:Panel>
    

    Please let me know if there is a better solution. Btw, I have a hunch that the basedOn attribute of State component is the way to go. But don't know what kind of format it expects:

        <s:State name="login" basedOn="???" />