Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Flex event Mechanism

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample analysis of Flex event mechanism", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of Flex event mechanism".

What is the Flex event mechanism

The Flex event can be seen as a trigger mechanism, which will be triggered when certain conditions are met. For example, MouseEvent refers to a series of events that are triggered when the mouse operates. Many controls have a click event, which is an instance of MouseEvent, and when the mouse is clicked, the system automatically throws a MouseEvent event named click (this method will be described later). If a method is registered on click at this time, it will be executed when the event is triggered.

Flex event Distribution in 1.Flex event Mechanism

In the end, all objects that inherit from EventDispatcher will contain the method dispatchEvent, which has a parameter, the event object.

The event registration channel mentioned earlier is just a channel, but in fact, events are distributed by this method, and the channel is just a channel.

His role is to distribute an event object, his distribution is aimless, a form of broadcast, Flex event listener thread will receive a variety of events (we call it capture events, which will be described later), then which is the event you want, identification will be distinguished by the event's type attribute.

◆ Flex event object

When a distribution event of the Flex event mechanism occurs, an event object is dispatched. No matter which event class it is, it inherits from the flash.events.Event object, which contains some important properties, type and bubbles.

Type is the type of event, and event listener uses this parameter to identify whether it is the event it is listening to.

Bubbles is a Boolean value that determines whether the object is passed up. The default is false. What do you mean? Just draw a picture.

For example, when the button component distributes the click event object and sets the bubbles to false, then its distribution looks like this

Cosmetic code

DispatchEvent (newMouseEvent ("click", false))

The event object cannot span the component itself, except, of course, the registration channel mentioned earlier (which is very vivid).

Therefore, if there is no registered channel, the events distributed by this button component cannot be captured in the Flex main application.

If we set Bubbles to true, that's what he looks like.

DispatchEvent (newMouseEvent ("click", true))

As you can see, this event can cross over the component itself to the Flex main application. Not only that, it is clearly stated in the help manual that if the event is not captured during the delivery process, the event will be uploaded layer by layer until the final stage, and if it has not been captured, the event will be destroyed.

In this way, even if we don't have an event channel for click, as long as we add a Flex event listener (addEventListener) to the Flex main application, we can get the distributed click event.

So, isn't it useless to register the channel? No, as mentioned before, the registration channel is current and visible, so if your component is to be used by others, it is very clear at a glance without knowing exactly what events are distributed in your source code. However, do not listen and register the same event, which will be repeated. (I'll talk about it later)

2.Flex event snooping

In the distribution of the Flex event mechanism, we said that if the trigger event is not called through the registration channel, then we need a listener to capture it. How to capture the distributed event is through the type value of the Flex event.

For example:

Some static constants are provided in the events of Flex, so let's call them in case we get the wrong number. So this sentence can be written like this.

TestBtn.addEventListener (MouseEvent.CLICK,clickHandler)

We see that there are no parameters passed in the listening callback method. Yes, this is somewhat different from the way the channel is written. The callback method here (that is, clickHandler) is just a reference and does not represent the execution of the method. What it means is to tell eventLinstener that if the click event is caught, then go to clickHandler and execute it, and the event object parameters are passed dynamically during execution. (if you are familiar with ajax, it should be easy to understand.)

This is how it works. If you register click's event channel again, then both of them will take effect, which is obviously superfluous.

3.Flex event mechanism about async and execution order

The previous statement is wrong, there is no concept of threads in as, in remote requests, the result events and error events are asynchronous. If you need to process the results, you need to use monitoring and get your remote data in the callback.

When dealing with local events, they are still synchronized.

The callback methods are executed in less order than even the methods after dispatchEvent. If the next method depends on the event callback, write the next method to the callback method.

The above is all the content of the article "sample Analysis of Flex event Mechanism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report