File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -760,6 +760,30 @@ test( "_on() with delegate", function() {
760760 $ . ui . testWidget ( ) ;
761761} ) ;
762762
763+ test ( "_on() with delegate to descendent" , function ( ) {
764+ expect ( 4 ) ;
765+ $ . widget ( "ui.testWidget" , {
766+ _create : function ( ) {
767+ this . target = $ ( "<p><strong>hello</strong> world</p>" ) ;
768+ this . child = this . target . children ( ) ;
769+ this . _on ( this . target , {
770+ "keyup" : "handlerDirect" ,
771+ "keyup strong" : "handlerDelegated"
772+ } ) ;
773+ this . child . trigger ( "keyup" ) ;
774+ } ,
775+ handlerDirect : function ( event ) {
776+ deepEqual ( event . currentTarget , this . target [ 0 ] ) ;
777+ deepEqual ( event . target , this . child [ 0 ] ) ;
778+ } ,
779+ handlerDelegated : function ( event ) {
780+ deepEqual ( event . currentTarget , this . child [ 0 ] ) ;
781+ deepEqual ( event . target , this . child [ 0 ] ) ;
782+ }
783+ } ) ;
784+ $ . ui . testWidget ( ) ;
785+ } ) ;
786+
763787test ( "_on() to common element" , function ( ) {
764788 expect ( 1 ) ;
765789 $ . widget ( "ui.testWidget" , {
Original file line number Diff line number Diff line change @@ -362,17 +362,19 @@ $.Widget.prototype = {
362362 } ,
363363
364364 _on : function ( element , handlers ) {
365+ var delegateElement ,
366+ instance = this ;
365367 // no element argument, shuffle and use this.element
366368 if ( ! handlers ) {
367369 handlers = element ;
368370 element = this . element ;
371+ delegateElement = this . widget ( ) ;
369372 } else {
370373 // accept selectors, DOM elements
371- element = $ ( element ) ;
374+ element = delegateElement = $ ( element ) ;
372375 this . bindings = this . bindings . add ( element ) ;
373376 }
374377
375- var instance = this ;
376378 $ . each ( handlers , function ( event , handler ) {
377379 function handlerProxy ( ) {
378380 // allow widgets to customize the disabled handling
@@ -396,7 +398,7 @@ $.Widget.prototype = {
396398 eventName = match [ 1 ] + instance . eventNamespace ,
397399 selector = match [ 2 ] ;
398400 if ( selector ) {
399- instance . widget ( ) . delegate ( selector , eventName , handlerProxy ) ;
401+ delegateElement . delegate ( selector , eventName , handlerProxy ) ;
400402 } else {
401403 element . bind ( eventName , handlerProxy ) ;
402404 }
You can’t perform that action at this time.
0 commit comments