User Guide
948 Chapter 7: 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” on page 415.)
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 939.
Note: For information about handling component events in a Flash application, see Chapter 4,
“Handling Component Events,” on page 55.
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 function 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.
You can explicitly build an event object before dispatching the event, as the following
example shows:
var eventObj = new Object();
eventObj.type = "myEvent";
eventObj.target = this;
dispatchEvent(eventObj);
You can also use a shortcut syntax that sets the value of the type property and the target
property and dispatches the event in a single line:
ancestorSlide.dispatchEvent({type:"revealChild", target:this});
In the preceding example, setting the target property is optional, because it is implicit.