Event mechanism in flex

Source: Internet
Author: User
1. Event Overview
Events can be triggered by peripherals, such as the keyboard, mouse, or external input, such as the return of web service.
Events can also be triggered when the widget's appearance and lifecycle change, such as creating or changing the widget size.
All user-to-application interactions generate events. Events may also occur if the user does not directly interact with the application, for example, data loading is complete.
You can use the event listener in the program to listen to these events. The event listener is a function method used to respond to specified events. It is also called an event processor.
The flex event model is based on the dom3 event model.
The component generates dispatch events and consumes (Listens) Other events. If an object wants to know information about other object events, you can register a listener.
When an event occurs, the object sends the event to all registered listeners.
The component has built-in events provided by Flex. You can also use the dispatch-listener model to define your own event listener and specify which events the listener listens.
II. Introduction to event streams
When an event is distributed, the event object starts scanning the display list from the root node to the target object and checks whether each node has a listener.
The target object refers to the objects that generate events in the display list. For example:


How to resize the vbox at this time, the system checks panel and hbox from the root (Application) until the target object-The vbox with the resize event is generated.

3. Event Distribution
In Flex, you can use the dispatchevent () method to manually dispatch events. This method can be called by all the subclass of uicomponent.
Syntax:
Objectinstance. dispatchevent (new event ("event_type"): Boolean
The event_type parameter is the type attribute of the event object. The return value of the function is always true.
You can use this method to dispatch any event, not just custom events. For example, you can dispatch a button click event.
VaR result: Boolean = buttoninstance. dispatchevent (new event (mouseevent. Click ));

In a flex application, it is not necessary to process the newly distributed event. If an event is triggered without a corresponding listener, flex ignores this event.
If you want to add a new attribute to the event object, you must inherit the event class and then define the new attribute.

4. Event propagation:
After an event is triggered, flex has three phases to detect the event listener. The sequence of the three phases is as follows:
1. Capture
2. Objectives
3. Floating
At any stage, nodes have the opportunity to operate events. For example, if a user clicks a button in the vbox,
In the capture phase, flex checks whether the application object (root node) and vbox have listeners to handle this event. Flex then triggers the button listener in the target phase.
In the floating phase, vbox and applications obtain the opportunity to process events again in the reverse order of the capture phase.
In actionscript3.0, you can register an event listener on any target node. However, some events are directly transmitted to the target node, such as the socket class.
The node sequence in the capture phase is from the parent node to the child node, while the floating phase is the opposite.
By default, the event capture function is disabled. to capture an event, you must explicitly specify the event to be processed during the capture phase.
Each event has the target and currenttarget attributes to help track the process of event propagation.

Capture phase:
In the capture phase, flex checks in the display list whether the event's ancestor has registered the event listener. Flex starts from the root node and goes down sequentially.
In most cases, the root node is the application object. At the same time, flex changes the currenttarget value of the event.
By default, no container listening event. use_capture parameter is set to false at this stage. The only way to add a listener at this stage is when add_listener is called,
Input a use_capture parameter with the value true, for example:
Myaccordion. addeventlistener (mouseevent. mouse_down, customlogevent, true );
If the listener is added to mxml, flex sets this parameter to false and there is no way to modify it.
If use_capture is set to true, the event will not go up. If you want to capture and float, you must call addeventlistener twice.
The use_capture parameter is true, and the use_capture parameter is false;
Capture is rarely used, and floating is more common.

Target phase:
In the target stage, flex triggers the event listener and does not check other nodes.

Floating stage:
The event is only floating when the bubbles attribute is true. Floating events include: Change, click, DoubleClick, keydown, keyup, mousedown, and mouseup.
In the floating stage, flex changes the currenttarget attribute of the event, and the target attribute is the object of the initial event distribution.

Event Query stage:
Use eventphase of the event to obtain the current phase of the event,
1: capture_phase
2: at_target
3: bubbling_phase
Example: Private function determinestate (Event: mouseevent): void {Debug. Trace (event. eventphase + ":" + event. currenttarget. ID );}

Stop propagation:
Use the following two functions to stop event propagation:
Stoppropagation ()
Stopimmediatepropagation ()

Source http://www.blogjava.net/usherlight/archive/2006/03/02/33264.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.