42
Creating Custom Events
eventObj.isEnabled=true;
dispatchEvent(eventObj);
For complete examples that create and dispatch custom events, see
Chapter 8, “Creating
Advanced MXML Components,” on page 91
and
Chapter 9, “Creating Simple Visual
Components in ActionScript,” on page 121
.
Creating static constants for the Event.type property
The constructor of an event class typically takes a single required argument that specifies the
value of the event object’s
type
property. In the previous section, you passed the string
enableChange
to the constructor, as the following example shows:
// Define event object, initialize it, then dispatch it.
var eventObj:EnableChangeEvent = new EnableChangeEvent("enableChange");
dispatchEvent(eventObj);
The Flex compiler does not examine the string passed to the constructor to determine if it is
valid. Therefore, the following code compiles, even though
enableChangeAgain
might not
be a valid value for the
type
property:
var eventObj:EnableChangeEvent =
new EnableChangeEvent("enableChangeAgain");
Because the compiler does not check the value of the
type
property, the only time that your
application can determine if
enableChangeAgain
is valid is at run time.
However, to ensure that the value of the
type
property is valid at compile time, Flex event
classes define static constants for the possible values for the
type
property. For example, the
Flex
EffectEvent
class defines the following static constant:
// Define static constant for event type.
public static const EFFECT_END:String = "effectEnd";
To create an instance of an EffectEvent class, you use the following constructor:
var eventObj:EffectEvent = new EffectEvent(EffectEvent.EFFECT_END);
If you incorrectly reference the constant in the constructor, the compiler generates a syntax
error because it cannot locate the associated constant. For example, the following constructor
generates a syntax error at compile time because
MY_EFFECT_END
is not a predefined constant
of the EffectEvent class:
var eventObj:EffectEvent = new EffectEvent(EffectEvent.
MY_EFFECT_END
);
Содержание FLEX 2 - CREATING AND EXTENDING COMPONENTS
Страница 1: ...Creating and Extending Flex 2 Components Adobe Flex 2...
Страница 6: ...6 Contents...
Страница 10: ...10 About Flex Documentation...
Страница 12: ......
Страница 24: ...24 Creating Flex Components...
Страница 74: ...74 Compiling Components...
Страница 76: ......
Страница 118: ...118 Creating Advanced MXML Components...
Страница 120: ......
Страница 182: ...182 Creating Advanced Visual Components in ActionScript...
Страница 194: ...194 Creating Custom Style Properties...
Страница 204: ...204 Creating Template Components...
Страница 206: ......
Страница 216: ...216 Creating Custom Formatters...
Страница 254: ...254 Index...