Flex application initialisation code using creationComplete

This is only a very small post, but something I initially struggled to find so I have deemed it worthy.

Compared to traditional UI coding, Flex hides the application flow entirely away from the programmer. You don’t need to worry about initialising the window objects, you don’t need to know about the event processing thread and you should not be concerned with how your components get drawn. All in all that is a fantastic thing. But I initially struggled to determine how to run some code once a Flex application has been loaded. For example in my latest (and soon to be posted about) project graffed, on initialisation I wanted to load the user details if they were cookied. It’s actually extremely easy, in your top level MXML mx:Application tag, simply add the attribute creationComplete=”init()”. Here init() refers to a function within the script data of the MXML file, e.g.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
    <mx:Script>
        <![CDATA[
        import mx.controls.Alert;

        private function init():void
        {
            Alert.show("Starting the program"):
        }
        ]]>
    </mx:Script>
</mx:Application>

Here, the program will run init() on launch and give you a nice informative message. Simple.

Leave a Reply

Your email address will not be published. Required fields are marked *