Skip to content

Commit

Permalink
Widget: Modified _trigger to invoke callbacks with apply so that hand…
Browse files Browse the repository at this point in the history
…lers are invoked the same way .trigger() invokes them. Fixes #6795 - Widget: _trigger passes array arguments to handlers inconsistently.
  • Loading branch information
Michael DellaNoce authored and scottgonzalez committed Feb 1, 2011
1 parent cb8f5b7 commit b3fcf17
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
48 changes: 48 additions & 0 deletions tests/unit/widget/widget_tickets.js
Expand Up @@ -40,4 +40,52 @@ test( "#5830 - Widget: Using inheritance overwrites the base classes options", f
delete $.ui.testWidgetExtension;
});

test( "#6795 - Widget: handle array arguments to _trigger consistently", function() {
expect( 4 );

$.widget( "ui.testWidget", {
_create: function() {},
testEvent: function() {
var ui = {
foo: "bar",
baz: {
qux: 5,
quux: 20
}
};
var extra = {
bar: 5
};
this._trigger( "foo", null, [ ui, extra ] );
}
});
$( "#widget" ).bind( "testwidgetfoo", function( event, ui, extra ) {
same( ui, {
foo: "bar",
baz: {
qux: 5,
quux: 20
}
}, "event: ui hash passed" );
same( extra, {
bar: 5
}, "event: extra argument passed" );
});
$( "#widget" ).testWidget({
foo: function( event, ui, extra ) {
same( ui, {
foo: "bar",
baz: {
qux: 5,
quux: 20
}
}, "callback: ui hash passed" );
same( extra, {
bar: 5
}, "callback: extra argument passed" );
}
})
.testWidget( "testEvent" );
});

}( jQuery ) );
11 changes: 8 additions & 3 deletions ui/jquery.ui.widget.js
Expand Up @@ -289,7 +289,8 @@ $.Widget.prototype = {
},

_trigger: function( type, event, data ) {
var callback = this.options[ type ];
var callback = this.options[ type ],
args;

event = $.Event( event );
event.type = ( type === this.widgetEventPrefix ?
Expand All @@ -309,8 +310,12 @@ $.Widget.prototype = {

this.element.trigger( event, data );

return !( $.isFunction(callback) &&
callback.call( this.element[0], event, data ) === false ||
args = $.isArray( data ) ?
[ event ].concat( data ) :
[ event, data ];

return !( $.isFunction( callback ) &&
callback.apply( this.element[0], args ) === false ||
event.isDefaultPrevented() );
}
};
Expand Down

0 comments on commit b3fcf17

Please sign in to comment.