File tree Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -760,6 +760,30 @@ test( "_on() with delegate", function() {
760
760
$ . ui . testWidget ( ) ;
761
761
} ) ;
762
762
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
+
763
787
test ( "_on() to common element" , function ( ) {
764
788
expect ( 1 ) ;
765
789
$ . widget ( "ui.testWidget" , {
Original file line number Diff line number Diff line change @@ -362,17 +362,19 @@ $.Widget.prototype = {
362
362
} ,
363
363
364
364
_on : function ( element , handlers ) {
365
+ var delegateElement ,
366
+ instance = this ;
365
367
// no element argument, shuffle and use this.element
366
368
if ( ! handlers ) {
367
369
handlers = element ;
368
370
element = this . element ;
371
+ delegateElement = this . widget ( ) ;
369
372
} else {
370
373
// accept selectors, DOM elements
371
- element = $ ( element ) ;
374
+ element = delegateElement = $ ( element ) ;
372
375
this . bindings = this . bindings . add ( element ) ;
373
376
}
374
377
375
- var instance = this ;
376
378
$ . each ( handlers , function ( event , handler ) {
377
379
function handlerProxy ( ) {
378
380
// allow widgets to customize the disabled handling
@@ -396,7 +398,7 @@ $.Widget.prototype = {
396
398
eventName = match [ 1 ] + instance . eventNamespace ,
397
399
selector = match [ 2 ] ;
398
400
if ( selector ) {
399
- instance . widget ( ) . delegate ( selector , eventName , handlerProxy ) ;
401
+ delegateElement . delegate ( selector , eventName , handlerProxy ) ;
400
402
} else {
401
403
element . bind ( eventName , handlerProxy ) ;
402
404
}
You can’t perform that action at this time.
0 commit comments