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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access