Macromedia flex 2 Manual

Página de 254
158
Creating Advanced Visual Components in ActionScript
// Call the createChildren() method of the superclass.
super.createChildren();
// Test for the existence of the children before creating them.
// This is optional, but do this so a subclass can create a different
// child.
if (!text_mc) {
text_mc = new TextArea();
text_mc.explicitWidth = 80;
text_mc.editable = false;
text_mc.addEventListener("change", handleChangeEvent);
// Add the child component to the custom component.
addChild(text_mc);
}
// Test for the existence of the children before creating them.
if (!mode_mc) {
mode_mc = new Button();
mode_mc.label = "Toggle Editing";
mode_mc.addEventListener("click", handleClickEvent);
// Add the child component to the custom component.
addChild(mode_mc);
}
}
Notice in this example that the 
createChildren()
 method calls the 
addChild()
 method to 
add the child component. You must call the 
addChild()
 method for each child object.
After you create a child component, you can use properties of the child component to define 
its characteristics. In this example, you create the Button and TextArea controls, initialize 
them, and register event listeners for them. You could also apply skins to the child 
components. For a complete example, see 
.
Implementing the commitProperties() method
You use the 
commitProperties()
 method to coordinate modifications to component 
properties. Most often, you use it with properties that affect how a component appears on the 
screen. 
Flex schedules a call to the 
commitProperties()
 method when a call to the 
invalidateProperties()
 method occurs. The 
commitProperties()
 method executes 
during the next 
render
 event after a call to the 
invalidateProperties()
 method. When 
you use the 
addChild()
 method to add a component to a container, Flex automatically calls 
the 
invalidateProperties()
 method.