User Guide

200 Creating Template Components
The following example shows an alternative implementation for the MyTemplateComponent
component shown in the section About template components” on page 195, named
MyTemplateComponentDeferred.mxml, by defining the topRow and bottomRow properties
to be of type IDeferredInstance:
<?xml version="1.0"?>
<!-- templating/myComponents/MyTemplateComponentDeferred.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.containers.HBox;
import mx.core.UIComponent;
// Define a deferred property for the top component.
public var topRow:IDeferredInstance;
// Define an Array of deferred properties
// for a row of components.
[ArrayElementType("mx.core.IDeferredInstance")]
public var bottomRow:Array;
private function init():void {
// Add the top component to the VBox container.
// Cast the IDeferredInstance object to UIComponent
// so that you can add it to the parent container.
addChild(UIComponent(topRow.getInstance()));
// Create an HBox container. This container
// is the parent container of the bottom row of components.
var controlHBox:HBox = new HBox();
// Add the bottom row of components
// to the HBox container.
for (var i:int = 0; i < bottomRow.length; i++)
controlHBox.addChild(UIComponent(bottomRow[i].getInstance()));
// Add the HBox container to the VBox container.
addChild(controlHBox);
}
]]>
</mx:Script>
</mx:VBox>
The IDeferredInstance interface defines a single method, getInstance(). Flex calls the
getInstance() method to initialize a property when it creates an instance of the component.
A subsequent call to the
getInstance() method returns a reference to the property value.