I need to know about Custom Events in Flex
package classes.events
{
import flash.events.Event;
public class ASSEvent extends Event
{
public static const ALERT:String = " Add Alert";
public function ASSEvent(type:String, bubbles:Boolean=true,cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
What does here type, bubbles and cancelable hold and why do we use it... can anyone explain the this entire code.
public function ASSEvent(ty开发者_StackOverflow中文版pe:String, bubbles:Boolean=true,cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
When bubbles
is true
and the event is dispatched from a UI element it will also be dispatched from the UI element's parent, then it's parent, and so forth until it reaches the top or a listener calls stopPropagation() or stopImmediatePropagation() on the event. If you are only dispatching the event on non-UI classes, just set bubbles to false.
When cancelable
is true
it basically means that you are expecting a listener to potentially call preventDefault(). You can check if someone has cancelled the event by calling isDefaultPrevented(). For example, you might create an "applying_data" event and allow a listener to cancel it to prevent the calling code actually applying the data. If the event is not logically cancellable, set it to false.
Please note that if you implement a custom event, you must override the clone() method.
More information on the Event class can be found at the Adobe docs.
The type field is the name of the event. Generally, you use constants from the Event or MouseEvent or KeyboardEvent classes (or one of many others), but you can also create your own. You should use static constants, but though you can use any String to represent one, try to keep them camelCase and leave out characters such as . and other punctuation. The reason being that, while using addEventListener works the same, you can define event listeners in MXML by using the name of the event...which won't work if you have dots and stuff.
The bubbles field should be set to true if you want your event to propogate up the chain of UI elements. For example, if a button fires the event, the Canvas its in would get the event to, and its HBox, and so on. This is handy when you want to catch events that could occur in a component's children, such as mouse clicks or keyboard clicks. This is why you can put such listeners on Application.application.stage and catch every keyDown your app gets.
The cancelable field lets you call preventDefault() on an event to cancel the default logic. This might include entering a character in a TextField, for example. If you want to do your own default processing, call addEventListener() with priority EventPriority.DEFAULT_HANDLER...then, other listeners can be created that, if they call preventDefault(), can enable you to NOT do the functionality you want by checking isDefaultPrevented().
精彩评论