]> source.dussan.org Git - jquery-ui.git/commitdiff
Widget: Fix _on to use element argument for delegated events. Fixes #8658 - Widget...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Wed, 24 Oct 2012 14:41:48 +0000 (10:41 -0400)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Wed, 24 Oct 2012 14:42:21 +0000 (10:42 -0400)
tests/unit/widget/widget_core.js
ui/jquery.ui.widget.js

index 624ccf74355b7ccccb4c41e1fac901026461a735..7bddc7f5c14df777c34184cb564a4f5781119efd 100644 (file)
@@ -760,6 +760,30 @@ test( "_on() with delegate", function() {
        $.ui.testWidget();
 });
 
+test( "_on() with delegate to descendent", function() {
+       expect( 4 );
+       $.widget( "ui.testWidget", {
+               _create: function() {
+                       this.target = $( "<p><strong>hello</strong> world</p>" );
+                       this.child = this.target.children();
+                       this._on( this.target, {
+                               "keyup": "handlerDirect",
+                               "keyup strong": "handlerDelegated"
+                       });
+                       this.child.trigger( "keyup" );
+               },
+               handlerDirect: function( event ) {
+                       deepEqual( event.currentTarget, this.target[ 0 ] );
+                       deepEqual( event.target, this.child[ 0 ] );
+               },
+               handlerDelegated: function( event ) {
+                       deepEqual( event.currentTarget, this.child[ 0 ] );
+                       deepEqual( event.target, this.child[ 0 ] );
+               }
+       });
+       $.ui.testWidget();
+});
+
 test( "_on() to common element", function() {
        expect( 1 );
        $.widget( "ui.testWidget", {
index eea12c471f9470e9ac072fed2c65dbd1d534fa5d..a6eae65800ac45aef5502bf5d2a87624f5f0fbfe 100644 (file)
@@ -362,17 +362,19 @@ $.Widget.prototype = {
        },
 
        _on: function( element, handlers ) {
+               var delegateElement,
+                       instance = this;
                // no element argument, shuffle and use this.element
                if ( !handlers ) {
                        handlers = element;
                        element = this.element;
+                       delegateElement = this.widget();
                } else {
                        // accept selectors, DOM elements
-                       element = $( element );
+                       element = delegateElement = $( element );
                        this.bindings = this.bindings.add( element );
                }
 
-               var instance = this;
                $.each( handlers, function( event, handler ) {
                        function handlerProxy() {
                                // allow widgets to customize the disabled handling
@@ -396,7 +398,7 @@ $.Widget.prototype = {
                                eventName = match[1] + instance.eventNamespace,
                                selector = match[2];
                        if ( selector ) {
-                               instance.widget().delegate( selector, eventName, handlerProxy );
+                               delegateElement.delegate( selector, eventName, handlerProxy );
                        } else {
                                element.bind( eventName, handlerProxy );
                        }