Commit 5c19701a authored by John Resig's avatar John Resig

Made it so that you can pass in an event object to the trigger data args and...

Made it so that you can pass in an event object to the trigger data args and it'll override the custom event object (this way you can pass in the event object of a mousemove to a drag event trigger, for example).
parent 042a4638
...@@ -134,9 +134,12 @@ jQuery.event = { ...@@ -134,9 +134,12 @@ jQuery.event = {
// Handle triggering a single element // Handle triggering a single element
} else { } else {
var val, ret, fn = jQuery.isFunction( element[ type ] || null ); var val, ret, fn = jQuery.isFunction( element[ type ] || null ),
// Check to see if we need to provide a fake event, or not
evt = !data[0] || !data[0].preventDefault;
// Pass along a fake event // Pass along a fake event
if ( evt )
data.unshift( this.fix({ type: type, target: element }) ); data.unshift( this.fix({ type: type, target: element }) );
// Trigger the event // Trigger the event
...@@ -147,6 +150,10 @@ jQuery.event = { ...@@ -147,6 +150,10 @@ jQuery.event = {
if ( !fn && element["on"+type] && element["on"+type].apply( element, data ) === false ) if ( !fn && element["on"+type] && element["on"+type].apply( element, data ) === false )
val = false; val = false;
// Extra functions don't get the custom event object
if ( evt )
data.shift();
// Handle triggering of extra function // Handle triggering of extra function
if ( extra && extra.apply( element, data ) === false ) if ( extra && extra.apply( element, data ) === false )
val = false; val = false;
......
...@@ -93,8 +93,8 @@ test("unbind(event)", function() { ...@@ -93,8 +93,8 @@ test("unbind(event)", function() {
ok( !el[0].$events, "Removed the events expando after all handlers are unbound." ); ok( !el[0].$events, "Removed the events expando after all handlers are unbound." );
}); });
test("trigger(event, [data]", function() { test("trigger(event, [data], [fn])", function() {
expect(28); expect(40);
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" );
...@@ -104,6 +104,13 @@ test("trigger(event, [data]", function() { ...@@ -104,6 +104,13 @@ test("trigger(event, [data]", 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 "test2";
};
// Simulate a "native" click // Simulate a "native" click
$("#firstp")[0].click = function(){ $("#firstp")[0].click = function(){
ok( true, "Native call was triggered" ); ok( true, "Native call was triggered" );
...@@ -114,8 +121,8 @@ test("trigger(event, [data]", function() { ...@@ -114,8 +121,8 @@ test("trigger(event, [data]", function() {
$("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]); $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
// Triggers handlers, native, and extra fn // Triggers handlers, native, and extra fn
// Triggers 9 // Triggers 8
$("#firstp").trigger("click", [1, "2", "abc"], handler); $("#firstp").trigger("click", [1, "2", "abc"], handler2);
// Simulate a "native" click // Simulate a "native" click
$("#firstp")[0].click = function(){ $("#firstp")[0].click = function(){
...@@ -123,12 +130,23 @@ test("trigger(event, [data]", function() { ...@@ -123,12 +130,23 @@ test("trigger(event, [data]", function() {
}; };
// Trigger only the handlers (no native) // Trigger only the handlers (no native)
// Triggers 4 // Triggers 5
equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" ); equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
// Trigger only the handlers (no native) and extra fn // Trigger only the handlers (no native) and extra fn
// Triggers 8 // Triggers 8
equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler), "test", "Verify handler response" ); equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), "test", "Verify handler response" );
// Build fake click event to pass in
var eventObj = jQuery.event.fix({ type: "click", target: document.body });
// Trigger only the handlers (no native), with external event obj
// Triggers 5
equals( $("#firstp").triggerHandler("foo", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
// Trigger only the handlers (no native) and extra fn, with external event obj
// Triggers 9
equals( $("#firstp").triggerHandler("foo", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
}); });
test("toggle(Function, Function)", function() { test("toggle(Function, Function)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment