User Guide

166 Creating Components
About invalidation
Macromedia recommends that a component not update itself immediately in most cases, but
that it instead should save a copy of the new property value, set a flag indicating what is
changed, and call the
invalidate() method. (This method indicates that just the visuals for
the object have changed, but size and position of subobjects have not changed. This method
calls the
draw() method.)
You must call an invalidation method at least once during the instantiation of your
component. The most common place for you to do this is in the
createChildren() or
layoutChildren() methods.
Dispatching events
If you want your component to broadcast events other than the events it may inherit from a
parent class, you must call the
dispatchEvent() method in the component’s class file.
The
dispatchEvent() method is defined in the mx.events.EventDispatcher class and is
inherited by all components that extend UIObject. (See “EventDispatcher class” in
Components Language Reference.)
You should also add an Event metadata tag at the top of the class file for each new event. For
more information, see About the Event tag” on page 153.
Using the dispatchEvent() method
In the body of your component’s ActionScript class file, you broadcast events using the
dispatchEvent() method. The dispatchEvent() method has the following syntax:
dispatchEvent(eventObj)
The eventObj parameter is an ActionScript object that describes the event (see the example
later in this section).
You must declare the
dispatchEvent() method in your code before you call it, as follows:
private var dispatchEvent:Function;
You must also create an event object to pass to dispatchEvent(). The event object contains
information about the event that the listener can use to handler the event.
NOTE
For information about handling component events in a Flash application, see Chapter 4,
“Handling Component Events,” on page 63.