Don't pass handler function as data for events + test (#1140)

This commit is contained in:
Brandon Aaron 2007-04-24 21:48:52 +00:00
parent 94dfccc6db
commit 11b64c1149
2 changed files with 15 additions and 10 deletions

View File

@ -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);
});
},

View File

@ -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"]);
});