Skip to content

Commit

Permalink
Fold dispatch function into main event dispatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin committed Oct 24, 2011
1 parent 470cc07 commit b208042
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions src/event.js
Expand Up @@ -68,7 +68,7 @@ jQuery.event = {
// Discard the second event of a jQuery.event.trigger() and
// when an event is called after a page has unloaded
return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
jQuery.event.handle.apply( eventHandle.elem, arguments ) :
jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
undefined;
};
// Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
Expand Down Expand Up @@ -405,16 +405,18 @@ jQuery.event = {
return event.result;
},

handle: function( event ) {
dispatch: function( event ) {

// Make a writable jQuery.Event from the native event object
event = jQuery.event.fix( event || window.event );

var handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []),
delegateCount = handlers.delegateCount,
args = [].slice.call( arguments, 0 ),
run_all = !event.exclusive && !event.namespace,
specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle,
handlerQueue = [],
i, cur, selMatch, matches, handleObj, sel, hit, related;
i, j, cur, ret, selMatch, matches, handleObj, sel, hit, related;

// Use the fix-ed jQuery.Event rather than the (read-only) native event
args[0] = event;
Expand Down Expand Up @@ -455,7 +457,30 @@ jQuery.event = {

for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
matched = handlerQueue[ i ];
dispatch( matched.elem, event, matched.matches, args );
for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) {
handleObj = matched.matches[ j ];

// Triggered event must either 1) be non-exclusive and have no namespace, or
// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {

// Pass in a reference to the handler function itself
// So that we can later remove it
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;

ret = ( specialHandle || handleObj.handler ).apply( matched.elem, args );

if ( ret !== undefined ) {
event.result = ret;
if ( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
}
}
}

return event.result;
Expand Down Expand Up @@ -594,46 +619,14 @@ jQuery.event = {
if ( bubble ) {
jQuery.event.trigger( e, null, elem );
} else {
jQuery.event.handle.call( elem, e );
jQuery.event.dispatch.call( elem, e );
}
if ( e.isDefaultPrevented() ) {
event.preventDefault();
}
}
};

// Run jQuery handler functions; called from jQuery.event.handle
function dispatch( target, event, handlers, args ) {
var run_all = !event.exclusive && !event.namespace,
specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle,
j, handleObj, ret;

for ( j = 0; j < handlers.length && !event.isImmediatePropagationStopped(); j++ ) {
handleObj = handlers[ j ];

// Triggered event must either 1) be non-exclusive and have no namespace, or
// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {

// Pass in a reference to the handler function itself
// So that we can later remove it
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;

ret = ( specialHandle || handleObj.handler ).apply( target, args );

if ( ret !== undefined ) {
event.result = ret;
if ( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
}
}
}

jQuery.removeEvent = document.removeEventListener ?
function( elem, type, handle ) {
if ( elem.removeEventListener ) {
Expand Down

0 comments on commit b208042

Please sign in to comment.