jquery event: closes #3772. The extra function on $.event.trigger isn't supported anymore.

This commit is contained in:
Ariel Flesler 2009-01-02 22:23:52 +00:00
parent d20e7fede1
commit 989e3f61f7
2 changed files with 6 additions and 52 deletions

View file

@ -163,7 +163,7 @@ jQuery.event = {
} }
}, },
trigger: function( event, data, elem, extra) { trigger: function( event, data, elem) {
// Event object or event type // Event object or event type
var type = event.type || event; var type = event.type || event;
@ -227,15 +227,6 @@ jQuery.event = {
// Extra functions don't get the custom event object // Extra functions don't get the custom event object
data.shift(); data.shift();
// Handle triggering of extra function
if ( extra && jQuery.isFunction( extra ) ) {
// call the extra function and tack the current return value on the end for possible inspection
var ret = extra.apply( elem, event.result == null ? data : data.concat( event.result ) );
// if anything is returned, give it precedence and have it overwrite the previous value
if ( ret !== undefined )
event.result = ret;
}
// Trigger the native events (except for clicks on links) // Trigger the native events (except for clicks on links)
if ( event.target === elem && fn && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) { if ( event.target === elem && fn && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
this.triggered = true; this.triggered = true;
@ -508,18 +499,18 @@ jQuery.fn.extend({
}); });
}, },
trigger: function( type, data, fn ) { trigger: function( type, data ) {
return this.each(function(){ return this.each(function(){
jQuery.event.trigger( type, data, this, fn ); jQuery.event.trigger( type, data, this );
}); });
}, },
triggerHandler: function( type, data, fn ) { triggerHandler: function( type, data ) {
if( this[0] ){ if( this[0] ){
var event = jQuery.Event(type); var event = jQuery.Event(type);
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
jQuery.event.trigger( event, data, this[0], fn ); jQuery.event.trigger( event, data, this[0] );
return event.result; return event.result;
} }
}, },

View file

@ -270,7 +270,7 @@ test("unbind(event)", function() {
}); });
test("trigger(type, [data], [fn])", function() { test("trigger(type, [data], [fn])", function() {
expect(46); expect(11);
var handler = function(event, a, b, c) { var handler = function(event, a, b, c) {
equals( event.type, "click", "check passed data" ); equals( event.type, "click", "check passed data" );
@ -280,28 +280,6 @@ test("trigger(type, [data], [fn])", function() {
return "test"; return "test";
}; };
var handler2 = function(a, b, c) {
equals( a, 1, "check passed data" );
equals( b, "2", "check passed data" );
equals( c, "abc", "check passed data" );
return false;
};
var handler3 = function(a, b, c, v) {
equals( a, 1, "check passed data" );
equals( b, "2", "check passed data" );
equals( c, "abc", "check passed data" );
equals( v, "test", "check current value" );
return "newVal";
};
var handler4 = function(a, b, c, v) {
equals( a, 1, "check passed data" );
equals( b, "2", "check passed data" );
equals( c, "abc", "check passed data" );
equals( v, "test", "check current value" );
};
var $elem = jQuery("#firstp"); var $elem = jQuery("#firstp");
// Simulate a "native" click // Simulate a "native" click
@ -313,10 +291,6 @@ test("trigger(type, [data], [fn])", function() {
// Trigger 5 // Trigger 5
$elem.bind("click", handler).trigger("click", [1, "2", "abc"]); $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
// Triggers handlers, native, and extra fn
// Triggers 9
$elem.trigger("click", [1, "2", "abc"], handler4);
// Simulate a "native" click // Simulate a "native" click
$elem[0].click = function(){ $elem[0].click = function(){
ok( false, "Native call was triggered" ); ok( false, "Native call was triggered" );
@ -326,9 +300,6 @@ test("trigger(type, [data], [fn])", function() {
// Triggers 5 // Triggers 5
equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" ); equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
// Trigger only the handlers (no native) and extra fn
// Triggers 8
equals( $elem.triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
var pass = true; var pass = true;
try { try {
jQuery('#form input:first').hide().trigger('focus'); jQuery('#form input:first').hide().trigger('focus');
@ -336,14 +307,6 @@ test("trigger(type, [data], [fn])", function() {
pass = false; pass = false;
} }
ok( pass, "Trigger focus on hidden element" ); ok( pass, "Trigger focus on hidden element" );
// have the extra handler override the return
// Triggers 9
equals( $elem.triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
// have the extra handler leave the return value alone
// Triggers 9
equals( $elem.triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
}); });
test("trigger(eventObject, [data], [fn])", function() { test("trigger(eventObject, [data], [fn])", function() {