The lesson is to explain how jquery implements its event system. The previous lesson mainly talked about JQuery.event.add source parsing, which is mainly for the jquery source code analysis, if you do not need to understand how jquery implementation of the event system mechanism, can be ignored. In this lesson, let's take a look at JQuery.event.remove's source code interpretation.
The purpose of the Remove method is to find the event queue based on the user's parameters, remove the matching Handleobj object from the inside, and possibly remove more than one or all of the handleobj in the case of insufficient parameters. When the length of the queue is 0 (when an event's event handler array is empty, which means that there is no event handler for this type of event), the corresponding event is removed when events is an empty object (events is a collection of all event types that the element binds to, and when it is empty, If the element is not bound to any event, then there is no need to save the element in the cache system's unique identity, then the UUID is cleared (that is, the element is the only identity in the cache system).
Next, let's look at its source code:
remove = function (elem,types,handler,selector) {
var t,tns,type,origtype,namespaces,origcount,j,events,special,eventtype,handleobj,
Elemdata = Jquery.hasdata (elem) && jquery._data (elem);
if (!elemdata | |!) ( Events = elemdata.events) { //If there is no data on this element in the cache system, or the addition of custom attributes (Jquery._data (elem)) is not supported, it is returned directly. If adding a custom attribute is supported and the cache system has data for this element, but if the event handler for the element is empty, it is also returned directly, because nothing can be removed, and the element itself is not bound to any event handlers.
Return
}
Types = Jquery.trim (Hoverhack (types| | "")). Split (""); //Handle hover event, it needs to convert to MouseEnter and MouseLeave two events
for (t=0;t<types.length;t++) {
....
Type = Origtype = Types[t];
....
if (!type) { //If you do not specify the type of event that you want to remove, remove all event types or all event types associated with this namespace
For (type in events) {
JQuery.event.remove (elem,type,handler,selector,true);
}
Continue
}
Special = Jquery.event.special[type] | | {}; //Handling special event types, not all events can be used directly, such as Firefox without MouseWheel, need to use Dommousescroll impersonate
Type = (selector? special.delegateType:special.bindType) | | Type //For event proxy binding, you need to handle events that cannot be bubbling, and get the type of event that is really used for binding
EventType = Events[type] | | []; //Gets an array of Mount event description objects of this event type
Origcount = Eventtype.length;
for (j=0;j<eventtype.length;j++) { //Remove Eligible event description objects in the divisor group
Handleobj = Eventtype[j];
if (origtype===handleobj.origtype&& //Compare event types are consistent
!handler | | Handler.guid = = = handleobj.guid&& //If there is no incoming event handler, or if it is passed in, and the GUID equals the GUID of the event description object, continue to Judge
!namespace | | Namespace.test (handleobj.namespace) && //If types have namespaces, determine if their namespaces are the same
!selector | | selector = = = Handleobj.selector //If it is an event proxy, determine if the event proxy is consistent,
){
Eventtype.splice (J--, 1); //Remove this event description object
if (handleobj.selector) { //If the event description object for the event agent is removed, reduce delegatecount by one
eventtype.delegatecount--;
}
.....
}
}
if (eventtype.length = = = 0 && origcount! ==eventtype.length) {//If the event description object array is empty and is empty after the for loop has been removed, enter the IF statement. The reason for the subsequent judgment is that the eventtype itself, if it is empty, is not removed through the for loop, and then into the IF statement, there will be a dead loop
.....
Delete Events[type]; //Delete the properties of this type of event. So there's no event handler for this type of event, and it's all removed.
}
}
if (Jquery.isemptyobject (events)) {//If the attribute of the event type is deleted for this element, then events will be an empty object
Delete Elemdata.handle; //Since none of the event types for this element proves that this element is bound to any events, the callback method added to it by add is also removed (this callback method invokes all callback methods for this element).
Jquery.removedata (Elem, "events", true); //Remove this element in the events space of the cache system (clear the UUID of this element in the cache space).
}
}
Next, let's take a look at the source code of JQuery.event.dispatch:
jquery is using this dispatch method to get the corresponding queue from the events object in the cache body, then fix the event object, execute in the callback of the incoming user, decide whether to stop the loop based on the return value of the callback, and whether to prevent the default behavior and event bubbling.
Dispatch = function (event) {
event = JQuery.event.fix (Event | | window.event); //solve the differences between the Internet Explorer and IE event objects, such as: W3c,event.target equals IE's event.srcelement
var i,j,cur,ret,selmatch,matched,matched,handleobj,sel,related,
Handlers = ((Jquery._data (This, "events") | | {}) [Event.type] | | [], //Get the Events object of this (action element) in the cache system, and then take its event type (the type of the event you triggered) Property object, which is the array containing the event description object.
Delegatecount = Handlers.delegatecount, //Get this event description object array How many of the event proxies are used
args = Core_slice.call (arguments), //[].slice.call (arguments), converting an array
...
Special = Jquery.event.special[event.type] | | {}, //Resolving special event Types
Handlerqueue = [];
....
if (Delegatecount &&!) ( Event.button && Event.type = = = "click") { //Here is the first Event.button this property, which indicates whether the user clicked the left or right mouse button or scroll bar, and so on. IE is not the same as the standard browser value, for example, left IE is 1, while the other browser is 0 and so on. But in the above code has fixed the event object, so ie, click the left button is 0. So the above if statement means: If there is an event proxy, and is left-click (event.button=0), enter the IF statement.
for (cur = event.target; cur! = This;cur = Cur.parentnode | | this) { //start with the event source, traverse all its ancestors, until the element that binds the event
if (cur.disabled!==true | | event.type!== click) {//If the element is forbidden and the event type is click, the IF statement is not entered. It means that the Click event cannot be triggered on a prohibited element .
....
matches = []; //To collect event description objects that meet the criteria
for (i=0;i<delegatecount;i++) { //Event description object with event proxy always in front of array
Handleobj = Handlers[i];
sel = Handleobj.selector; //Gets the execution context of this event description object
if (Selmatch[sel] ===undefined) { ///First time, Selmatch[sel] is undefined
Selmatch[sel] = Jquery.find (Sel,this,null,[cur]). length; //sel, the current event describes the object's execution context, the This event-triggered element, and cur the currently traversed element. If the element that is currently traversed is cur, it is true, and the mismatch is false.
}
if (Selmatch[sel]) { //if the current element matches in the event description object
Matches.push (Handleobj); //The event description object is collected. How many event description objects are collected when the number of elements is matched in the event description object array.
}
}
if (matches.length) {
Handlerqueue.push ({elem:cur,matches:matches}); //If the currently traversed element, the matching event describes one or more of the object arrays. Assemble these into a JSON object to store them.
}
}
}
}
if (Handlers.length > Delegatecount) { ///If there is an event description object that is not an event proxy, these event description objects are assembled into JSON with the elements of the bound event, stored
Handlerqueue.push ({elem:this,matches:handlers.slice (Delegatecount)});
}
For (I=0;i//traversal handlerqueue array, The condition is that the event does not stop bubbling.
matched = Handlerqueue[i];
Event.currenttarget = Matched.elem; the current target element that the//event bubbled to.
For (J=0;j<matched.matches.length &&!event.isimmediatepropagationstopped (); j + +) {// Iterates over all the current target elements in an array of event description objects of the same type as Event.type. The condition is that the user does not perform a stop bubbling method. (The Isimmediatepropagationstopped method returns true if the user calls the Stopimmediatepropagation method)
Handleobj = Matched.matches[j]; //Fetch to this event description object
.....
Event.data = Handleobj.data;
Event.handleobj = Handleobj; //Assign the event description object to the Handleobj property of the event
ret = ((Jquery.event.special[handleobj.origtype] | | {}). Handle | | Handleobj.handler). Apply (Matched.elem,args); ///Look at this event to describe whether the event type in the object is a special event, and if so, call its handle method. If not, call the handler method of the event description object directly. The execution context is in the target element of the current traversal.
if (ret!==undefined) {
Event.result = ret; //If the return value is not undefined after the event handler is executed, the result property assigned to event
if (ret = = = False) { //If the event handler returns false
Event.preventdefault (); //block default events
Event.stoppropagation (); //Stop bubbling
}
}
}
}
return event.result; //Returns the return value after the event handler executes.
}
The difficulty of dispatch is that the mechanism of simulating event propagation is actually the mechanism of event bubbling.
Come on!
24th Lesson: Event System 3