From 11b64c1149a0d49d105b50a2869d74cbe18613ed Mon Sep 17 00:00:00 2001 From: Brandon Aaron Date: Tue, 24 Apr 2007 21:48:52 +0000 Subject: [PATCH] Don't pass handler function as data for events + test (#1140) --- src/event/event.js | 10 +++++----- src/event/eventTest.js | 15 ++++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/event/event.js b/src/event/event.js index 3834fd65f..2d9c53056 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -14,7 +14,7 @@ jQuery.event = { element = window; // if data is passed, bind to handler - if( data != undefined ) { + if( data != handler ) { // Create temporary function pointer to original handler var fn = handler; @@ -55,7 +55,7 @@ jQuery.event = { if (element.addEventListener) element.addEventListener(type, element.$handle, false); else if (element.attachEvent) - element.attachEvent("on" + type, element.$handle, false); + element.attachEvent("on" + type, element.$handle); } // Add the function to the element's handler list @@ -101,7 +101,7 @@ jQuery.event = { if (element.removeEventListener) element.removeEventListener(type, element.$handle, false); else if (element.detachEvent) - element.detachEvent("on" + type, element.$handle, false); + element.detachEvent("on" + type, element.$handle); ret = null; delete events[type]; } @@ -274,7 +274,7 @@ jQuery.fn.extend({ */ bind: function( type, data, fn ) { return this.each(function(){ - jQuery.event.add( this, type, fn || data, data ); + jQuery.event.add( this, type, fn || data, fn && data ); }); }, @@ -309,7 +309,7 @@ jQuery.fn.extend({ jQuery.event.add( this, type, function(event) { jQuery(this).unbind(event); return (fn || data).apply( this, arguments); - }, data); + }, fn && data); }); }, diff --git a/src/event/eventTest.js b/src/event/eventTest.js index d6d116099..8d8321618 100644 --- a/src/event/eventTest.js +++ b/src/event/eventTest.js @@ -1,12 +1,12 @@ module("event"); test("bind()", function() { - expect(10); + expect(11); var handler = function(event) { ok( event.data, "bind() with data, check passed data exists" ); ok( event.data.foo == "bar", "bind() with data, Check value of passed data" ); - } + }; $("#firstp").bind("click", {foo: "bar"}, handler).click(); reset(); @@ -15,9 +15,14 @@ test("bind()", function() { ok( event.data.foo == "bar", "Check value of passed data" ); ok( data, "Check trigger data" ); ok( data.bar == "foo", "Check value of trigger data" ); - } + }; $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]); + var handler = function(event) { + ok ( !event.data, "Check that no data is added to the event object" ); + }; + $("#firstp").unbind().bind("click", handler).trigger("click"); + // events don't work with iframes, see #939 var tmp = document.createElement('iframe'); document.body.appendChild( tmp ); @@ -39,7 +44,7 @@ test("bind()", function() { var counter = 0; function selectOnChange(event) { equals( event.data, counter++, "Event.data is not a global event object" ); - } + }; $("select").each(function(i){ $(this).bind('change', i, selectOnChange); }).trigger('change'); @@ -90,7 +95,7 @@ test("trigger(event, [data]", function() { ok( a == 1, "check passed data" ); ok( b == "2", "check passed data" ); ok( c == "abc", "check passed data" ); - } + }; $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]); }); -- 2.39.5