Javascript front-end UI framework Kit User Guide-kitjs event management, kitkitjs

Source: Internet
Author: User

Javascript front-end UI framework Kit User Guide-kitjs event management, kitkitjs

Starting from today's chapter, I will focus on the event management of KitJs, try to use plain language to reveal how the mainstream js framework implements its own independent event management function internally.

(1) Common Dom events

We generally support writing events in HTML

<A onclick = "alert (1)"> test </a>

Or bind it to the dom object.

Document. getElementById ('A'). onclick = function () {alert (1 )}

Or Level 2 event

Document. getElementById ('A'). addEventListener ('click', function () {alert (1)}, flase)

Or use the script tag

<Script for = "a" event = "onclick"> alert (1) </script>

The W3C standard recommends binding the third method above. The second-level event method is used to decouple the strong dependency between HTML and Js.

(2) Problems

However, if we only use method 3 for Js programming, it is not enough because we will encounter the following problems:

1. browser compatibility. The names and parameters of browsers supported by the IE series and W3C for second-level event binding are inconsistent.

2. After binding level 2 events, you cannot know whether events, events, and content have been bound to the same element?

3. After being triggered by a level-2 event binding method, the sequence is not in the order prior to binding, but is executed randomly. Sometimes, we need to trigger the Method in order.

4. When an event of the same element is triggered, other events bound to the same element can be stopped without w3c standard APIs. w3c supports stopping bubbling.

5. In many cases, we use the anonymous Function Method to register Level 2 events without leaving a handle to register the event execution method. Therefore, it is difficult to use removeEventListener to deregister the event.

(3) how to solve the problem with Kit

Okay. The js framework exists to solve the above problems. Let's see how kit handles the above problems.

In the api of kit. js, an ev (config) method exists.

This method accepts a Map object, which contains four important parameters,

Elements to be bound by el

String Event Type

Fn trigger execution Method

Scope can be omitted. Do you need to specify the this pointer? If not, the el at registration is passed as the this pointer.

(4) code parsing

Let's further look at the code implementation

Starting from the core

If the input parameter is not empty, create an object on el of the input parameter to save the event registration evReg of KitJs.

The evReg object contains two sub-objects: evRegEv, which stores registration events.

In the evRegEv object, save a key as the current registration event, and the value is an array. In the array, put the config parameter passed in by method ev in the order of first arrival and last arrival. Note that, this is an array !!! This is important because Arrays can save order.

There is also an anonymous method called evRegFn to save event triggering,

We can see that evRegFn is an anonymous event. At the beginning, it will judge the global variable window [me. CONSTANTS. KIT_EVENT_STOPIMMEDIATEPROPAGATION] whether = true. If it is true, it will return and will not continue execution.

Next, he will receive the EV object triggered by the event, and append many objects to this EV in mergeIf mode, such as target, currentTarget, and relatedTarget, to solve browser compatibility problems.

StopNow, stopDefault, and stopGoOn are created to prevent the event from being triggered.

The following section is the key to evRegFn. We will loop through the event array in the previously created evRegEv and retrieve the config parameter passed in by the previous ev method in sequence, execute the method in the config parameter. If the return value of the method is not empty, return its return value.

Finally, make a browser compatible, bind our evRegFn anonymous method in the form of level 2 events.

(5) Summary

In short, Kit uses its own anonymous method to cache the event registration handle to an array, so that you can remember the order of events, it also provides an entry to identify previously registered events, parameters, methods, and so on, and provides compatibility with browser compatibility.

(6) deregister an event

With the help of Kit to cache the event handle, it is easy to log out.

You can see that Kit finds the corresponding event config through direct comparison, fn. toString comparison, and fn. toString (). trim () comparison, and deletes it from the array.

(7) event Enhancement

We should also note that the Kit has performed a mergeIf operation on the system Event object. First, why do we need to perform megerIf? Because the object attribute of the system Event is Readonly and cannot be overwritten, only attributes that do not exist can be added.

Therefore, the Kit can only be megerIf. We all know that the Event Object of Each browser has an incompatibility, so we need the Kit to fix these incompatibility. For example, IE has no target attribute and only srcElement, we can add the target attribute to realize W3c standard compatibility.

Of course, the repair alone cannot meet our needs. In many cases, we also need to make a small addition to the Event object.

Except getTouches [0]. clientX, this code is not compatible on the PC once the anonymous function is like this,

What should we do? It doesn't matter. We can give Event Object mergeIf our own attributes

FirstFingerClientX and so on. In this way, we can easily unify the code developed on the Mobile End and PC end.

Including, the next article is about HTML5 drag-and-drop. Advanced gesture events are all based on this architecture.

To add this question, why is it not as new as an Event in ExtJs?

1. The native objects of the system have inheritance relationships and do not want to destroy them.

2. If you use your new Object, it may cause the code to be removed from the framework and become unportable. You need to change the code content again.

Related Article

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.