Why test for the existence of children before creating them in createChildren

Testing for the existence of the children before creating them in createChildren is a documented best practice.

The more common reason for this is, like documented, allowing a subclass to create a different child.
The Panel is a good example of this, allowing to create a different header by replacing it's protected titleBar.


However, even if your'e not planning on allowing creating a different child, there's another good reason for you to still do so.

CreateChildren is a function called on the attachment phase of a component (attached by addChild).

Attachment obviously occurs on the first time your component is being added to the display list, but its important to realize that youre createChildren code might run more than once during the lifetime of your component (mentioned here as well). For example, when your component is re-attached (removed and added again).


Children of MXML components, which are created using createComponentFromDescriptor() will not get re-created since the function checks for existence (using hasChildMatchingDescriptor).









Share this article: