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 12-16 illustrates this. The trace() statement outputs null because button is not yet defined when the application first starts.

Example 12-16. 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:Script>
        <![CDATA[

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

        ]]>
    </mx:Script>

    <mx:states>
        <mx:State name="example">
            <mx:AddChild>
                <mx:Button id="button" label="Example" />
            </mx:AddChild>
        </mx:State>
    </mx:states>

</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 12-17 illustrates how that works. Because the creation policy of the button is now set to all, the trace() statement outputs the reference to the component.

Example 12-17. Understanding object creation policies: Policy all

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="initializeHandler(event)">

    <mx:Script>
        <![CDATA[

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

        ]]>
    </mx:Script>

    <mx:states>
        <mx:State name="example">
            <mx:AddChild creationPolicy="all">
                <mx:Button id="button" label="Example" />
            </mx:AddChild>
        </mx:State>
    </mx:states>

</mx:Application>

When the creation policy is set to none, the component isn’t instantiated until you explicitly call the createInstance() method of the AddChild object. If you’re defining the AddChild object using the <mx:AddChild> tag, you must assign an id. Example 12-18 illustrates how the none creation policy works. The first trace() statement outputs null because the component hasn’t been instantiated. The second trace() statement outputs the reference to the component because it is called immediately following the call to createInstance().

Example 12-18. Understanding object creation policy: Policy none

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="initializeHandler(event)">
    <mx:Script>
        <![CDATA[

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

        ]]>
    </mx:Script>

    <mx:states>
        <mx:State name="example">
            <mx:AddChild creationPolicy="none" id="exampleAddChild"
>

                <mx:Button id="button" label="Example" />
            </mx:AddChild>
        </mx:State>
    </mx:states>

</mx:Application>

In most applications, the default (auto) creation policy is the correct setting. However, there are several reasons you might want to select a different creation policy. The all policy, for example, is useful in at least two scenarios:

The added component requires a long time to initialize

If the component isn’t initialized until the state is requested, the user might experience a delay. By setting the policy to all, the component is initialized when the application first starts; that should mitigate any issues related to component initialization times and delays when changing states.

You want to reference the added component before first requesting the state

For example, when you create a component with several states, you might want to reference added components via an accessor method.

The none policy may not seem immediately useful; however, consider that the auto and all policies are very black and white:

  • When you select auto, components aren’t instantiated until the state is requested.

  • When you select all, components are instantiated when the application initializes.

There are reasons you might want to ensure that a component initializes before a state is requested, but you don’t want to force the component to instantiate when the application initializes. For example, a complex application might contain many states, each having components that take a long time to initialize. If you set the creation policy of all the AddChild objects to all, the user might have to wait a long time before the application initializes. However, it might be a better user experience if the application starts right away while the components for the rest of the states initialize in the background. Using the none creation policy allows you to do just that. Simply call the createInstance() method of the AddChild object to instantiate the child component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset