CreationComplete and ObjectUtil.getClassInfo

I just ran into a not so nice bug and really hope it doesn’t get anyone else. I have a custom component that I used in multiple places in my application. This component had different function calls that need to be made during the component’s startup, specifically the preInitialize, Initialize, and creationComplete event. To make sure that these function were all firing correctly I added some trace statements and the hilarity (sarcasm) started.

A very basic test case of my component looked as such:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
    width="400" height="300"
    preinitialize="this.onPreInitialize(event)"
    initialize="this.onInitialize(event)"
    creationComplete="this.onCreationComplete(event)">
    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;
            import mx.events.FlexEvent;

            private function onPreInitialize(event:FlexEvent):void
            {
                trace(event.type);
            }

            private function onInitialize(event:FlexEvent):void
            {
                trace(event.type);

            }

            private function onCreationComplete(event:FlexEvent):void
            {
                trace(event.type);

            }
        ]]>
    </mx:Script>
</mx:Canvas>

Due to the order of calls, I wanted to make sure that each of my components would be called in the correct order, so I added some more information to my trace statements. So my new code looked as such:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
    width="400" height="300"
    preinitialize="this.onPreInitialize(event)"
    initialize="this.onInitialize(event)"
    creationComplete="this.onCreationComplete(event)">
    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;
            import mx.events.FlexEvent;

            private function onPreInitialize(event:FlexEvent):void
            {
                trace(event.type);
                var o:Object = ObjectUtil.getClassInfo(this);
                trace(o.name);
            }

            private function onInitialize(event:FlexEvent):void
            {
                trace(event.type);
                var o:Object = ObjectUtil.getClassInfo(this);
                trace(o.name);
            }

            private function onCreationComplete(event:FlexEvent):void
            {
                trace(event.type);
                var o:Object = ObjectUtil.getClassInfo(this);
                trace(o.name);
            }
        ]]>
    </mx:Script>
</mx:Canvas>

I had expected something similar to:

preinitialize
initialize
creationComplete

But what I got was:

preinitialize
creationComplete
initialize

The component’s functions did not fire in the correct order and this caused extreme problems with my application (as you could guess). I couldn’t understand why this would happen and tested everything I could think of or was suggested to test (such as bubbling events from children components). Finally I got “lazy”/”smart” and changed my code to look like:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
    width="400" height="300"
    preinitialize="this.onPreInitialize(event)"
    initialize="this.onInitialize(event)"
    creationComplete="this.onCreationComplete(event)">
    <mx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private function onPreInitialize(event:FlexEvent):void
            {
                trace(event.type);
                trace("Tab");
            }

            private function onInitialize(event:FlexEvent):void
            {
                trace(event.type);
                trace("Tab");
            }

            private function onCreationComplete(event:FlexEvent):void
            {
                trace(event.type);
                trace("Tab");
            }
        ]]>
    </mx:Script>
</mx:Canvas>

And BAM!

preinitialize
Tab
initialize
Tab
creationComplete
Tab

Thank you ObjectUtil.getClassInfo… you now made the list.

Share

Comments (2)

martin_lsJune 18th, 2009 at 10:18 pm

Seems any call to getClassInfo (before onCreationComplete), will actually force onCreationComplete to be triggerd…which, kindof makes sense i guess. An interesting find non the less.

[...] CreationComplete and ObjectUtil.getClassInfo | The World In A State of Flex. [...]

Leave a comment

Your comment