O'Reilly logo

Programming Flex 2 by Joey Lott, Chafic Kazoun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Managing Object Creation Policies (Preloading Objects)

By default, components added by nonbase states aren't instantiated until the state is first requested. The MXML in Example 10-14 illustrates this. The trace( ) statement outputs null because button is not yet defined when the application first starts.

Example 10-14. Understanding object creation policies: default policy

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initializeHandler(event)">
    <mx:states>
        <mx:State name="example">
            <mx:AddChild>
                <mx:Button id="button" label="Example" />
            </mx:AddChild>
        </mx:State>
    </mx:states>
    <mx:Script>
        <![CDATA[

            private function initializeHandler(event:Event):void {
                trace(button);
            }

        ]]>
    </mx:Script>
</mx:Application>

However, you can manage when components added by states are instantiated using a creation policy. The default creation policy setting is auto, which means the component is instantiated when the state is first requested. You can set creation policies for each added component using the creationPolicy attribute of the <mx:AddChild> tag, or the creationPolicy property of the AddChild class. The possible values are auto (default), all, and none.

When you set the creation policy of an added component to all, the component is instantiated when the application first starts. The MXML in Example 10-15 illustrates how that works. Because the creation policy of the button is now set to all, the trace( ) statement outputs ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required