]> source.dussan.org Git - jquery.git/commitdiff
Make event.currentTarget the delegate node, always.
authorDave Methvin <dave.methvin@gmail.com>
Fri, 21 Oct 2011 15:11:36 +0000 (11:11 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 24 Oct 2011 15:18:14 +0000 (11:18 -0400)
This lets us use currentTarget for its intended use and avoids creating a non-standard delegateTarget property.

src/event.js
test/unit/event.js

index 38fd82b5e22fbb3a810bfd72c87acd51d2eee250..ff38d000d555927787fbedf3b19c1ae1137cbe05 100644 (file)
@@ -418,6 +418,7 @@ jQuery.event = {
 
                // Use the fix-ed jQuery.Event rather than the (read-only) native event
                args[0] = event;
+               event.currentTarget = this;
 
                // Determine handlers that should run if there are delegated events
                // Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
@@ -447,21 +448,15 @@ jQuery.event = {
                        }
                }
 
-               // Copy the remaining (bound) handlers in case they're changed
-               handlers = handlers.slice( delegateCount );
+               // Add the remaining (directly- bound) handlers
+               if ( handlers.length ) {
+                       handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) });
+               }
 
-               // Run delegates first; they may want to stop propagation beneath us
-               event.delegateTarget = this;
                for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
                        matched = handlerQueue[ i ];
                        dispatch( matched.elem, event, matched.matches, args );
                }
-               delete event.delegateTarget;
-
-               // Run non-delegated handlers for this level
-               if ( handlers.length && !event.isPropagationStopped() ) {
-                       dispatch( this, event, handlers, args );
-               }
 
                return event.result;
        },
@@ -613,7 +608,6 @@ function dispatch( target, event, handlers, args ) {
                specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle,
                j, handleObj, ret;
 
-       event.currentTarget = target;
        for ( j = 0; j < handlers.length && !event.isImmediatePropagationStopped(); j++ ) {
                handleObj = handlers[ j ];
 
@@ -932,7 +926,7 @@ jQuery.fn.extend({
                if ( one === 1 ) {
                        origFn = fn;
                        fn = function( event ) {
-                               jQuery.event.remove( event.delegateTarget || this, event );
+                               jQuery.event.remove( event.currentTarget || this, event );
                                return origFn.apply( this, arguments );
                        };
                        // Use same guid so caller can remove using origFn
index cce9061edab6e05e610adcd18ed2dbf9afb4b823..905e6ab99b15ef1b3bb1e3e7b80b805e428d2697 100644 (file)
@@ -1476,7 +1476,7 @@ test(".live()/.die()", function() {
        // Test this, target and currentTarget are correct
        jQuery("span#liveSpan1").live("click", function(e){
                equals( this.id, "liveSpan1", "Check the this within a live handler" );
-               equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a live handler" );
+               equals( e.currentTarget, document, "Check the event.currentTarget within a live handler" );
                equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a live handler" );
        });
 
@@ -2008,7 +2008,7 @@ test(".delegate()/.undelegate()", function() {
        // Test this, target and currentTarget are correct
        jQuery("#body").delegate("span#liveSpan1", "click", function(e){
                equals( this.id, "liveSpan1", "Check the this within a delegate handler" );
-               equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a delegate handler" );
+               equals( e.currentTarget, document.body, "Check the event.currentTarget within a delegate handler" );
                equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a delegate handler" );
        });