Widget: Fix _on to use element argument for delegated events. Fixes #8658 - Widget: this._on delegates using instance.widget() instead of passed element

This commit is contained in:
Jörn Zaefferer 2012-10-24 10:41:48 -04:00
parent 848ab48583
commit 721a4b4ae0
2 changed files with 29 additions and 3 deletions

View File

@ -760,6 +760,30 @@ test( "_on() with delegate", function() {
$.ui.testWidget(); $.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() { test( "_on() to common element", function() {
expect( 1 ); expect( 1 );
$.widget( "ui.testWidget", { $.widget( "ui.testWidget", {

View File

@ -362,17 +362,19 @@ $.Widget.prototype = {
}, },
_on: function( element, handlers ) { _on: function( element, handlers ) {
var delegateElement,
instance = this;
// no element argument, shuffle and use this.element // no element argument, shuffle and use this.element
if ( !handlers ) { if ( !handlers ) {
handlers = element; handlers = element;
element = this.element; element = this.element;
delegateElement = this.widget();
} else { } else {
// accept selectors, DOM elements // accept selectors, DOM elements
element = $( element ); element = delegateElement = $( element );
this.bindings = this.bindings.add( element ); this.bindings = this.bindings.add( element );
} }
var instance = this;
$.each( handlers, function( event, handler ) { $.each( handlers, function( event, handler ) {
function handlerProxy() { function handlerProxy() {
// allow widgets to customize the disabled handling // allow widgets to customize the disabled handling
@ -396,7 +398,7 @@ $.Widget.prototype = {
eventName = match[1] + instance.eventNamespace, eventName = match[1] + instance.eventNamespace,
selector = match[2]; selector = match[2];
if ( selector ) { if ( selector ) {
instance.widget().delegate( selector, eventName, handlerProxy ); delegateElement.delegate( selector, eventName, handlerProxy );
} else { } else {
element.bind( eventName, handlerProxy ); element.bind( eventName, handlerProxy );
} }