Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
178 CHAPTER 11 You can pass any data type to a property of type IDeferredInstance. In the example in the section "About template components" on page 173, you pass a Label control to the topRow property, and three Button controls to the bottomRow property. Notice in the example that the addChild() methods that take topRow and bottomRow as arguments cast them to UIComponent. This cast is necessary because the addChild() method can only add an object that implements the IUIComponent interface to a container, and the DeferredInstance.getInstance() method returns a value of type Object. Defining properties using the IDeferredInstance interface You can define component properties of type IDeferredInstance. Defining a generic property To define a generic property, one with no associated data type, you define its type as IDeferredInstance, as the following example shows: // Define a deferred property for the top component. public var topRow:IDeferredInstance; The user of the component can then specify an object of any type to the property. It is your responsibility in the component implementation to verify that the value passed by the user is of the correct data type. Restricting the data type of a property You use the [InstanceType] metadata tag to specify the allowed data type of a property of type IDeferredIn- stance, as the following example shows: // Define a deferred property for the top component. [InstanceType("mx.controls.Label")] public var topRow:IDeferredInstance; The Flex compiler validates that users only assign values of the specified type to the property. In this example, if the component user sets the topRow property to a value of a type other than mx.controls.Label, the compiler issues an error message. Defining an array of template properties You can define an Array of template properties, as the following example shows: // Define an Array of deferred properties for a row of components. // Do not restrict the type of the component. [ArrayElementType("mx.core.IDeferredInstance")] public var bottomRow:Array;