jquery event: closes #5250. bind(), unbind() and one() support Object Literals (needs some refactor though)
This commit is contained in:
parent
97e134fe80
commit
9ebb2fc654
2 changed files with 78 additions and 0 deletions
25
src/event.js
25
src/event.js
|
@ -564,7 +564,16 @@ jQuery.each({
|
|||
});
|
||||
|
||||
jQuery.fn.extend({
|
||||
// TODO: make bind(), unbind() and one() DRY!
|
||||
bind: function( type, data, fn, thisObject ) {
|
||||
// Handle object literals
|
||||
if ( typeof type === "object" ) {
|
||||
for ( var key in type ) {
|
||||
this.bind(key, data, type[key], fn);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
if ( jQuery.isFunction( data ) ) {
|
||||
thisObject = fn;
|
||||
fn = data;
|
||||
|
@ -577,6 +586,14 @@ jQuery.fn.extend({
|
|||
},
|
||||
|
||||
one: function( type, data, fn, thisObject ) {
|
||||
// Handle object literals
|
||||
if ( typeof type === "object" ) {
|
||||
for ( var key in type ) {
|
||||
this.one(key, data, type[key], fn);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
if ( jQuery.isFunction( data ) ) {
|
||||
thisObject = fn;
|
||||
fn = data;
|
||||
|
@ -593,6 +610,14 @@ jQuery.fn.extend({
|
|||
},
|
||||
|
||||
unbind: function( type, fn ) {
|
||||
// Handle object literals
|
||||
if ( typeof type === "object" && !type.preventDefault ) {
|
||||
for ( var key in type ) {
|
||||
this.unbind(key, type[key]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
return this.each(function() {
|
||||
jQuery.event.remove( this, type, fn );
|
||||
});
|
||||
|
|
|
@ -45,6 +45,59 @@ test("bind(), no data", function() {
|
|||
jQuery("#firstp").bind("click", handler).trigger("click");
|
||||
});
|
||||
|
||||
test("bind/one/unbind(Object)", function(){
|
||||
expect(6);
|
||||
|
||||
var clickCounter = 0, mouseoverCounter = 0;
|
||||
function handler(event) {
|
||||
if (event.type == "click")
|
||||
clickCounter++;
|
||||
else if (event.type == "mouseover")
|
||||
mouseoverCounter++;
|
||||
};
|
||||
|
||||
function handlerWithData(event) {
|
||||
if (event.type == "click")
|
||||
clickCounter += event.data;
|
||||
else if (event.type == "mouseover")
|
||||
mouseoverCounter += event.data;
|
||||
};
|
||||
|
||||
function trigger(){
|
||||
$elem.trigger("click").trigger("mouseover");
|
||||
}
|
||||
|
||||
var $elem = jQuery("#firstp")
|
||||
// Regular bind
|
||||
.bind({
|
||||
click:handler,
|
||||
mouseover:handler
|
||||
})
|
||||
// Bind with data
|
||||
.one({
|
||||
click:handlerWithData,
|
||||
mouseover:handlerWithData
|
||||
}, 2 );
|
||||
|
||||
trigger();
|
||||
|
||||
equals( clickCounter, 3, "bind(Object)" );
|
||||
equals( mouseoverCounter, 3, "bind(Object)" );
|
||||
|
||||
trigger();
|
||||
equals( clickCounter, 4, "bind(Object)" );
|
||||
equals( mouseoverCounter, 4, "bind(Object)" );
|
||||
|
||||
jQuery("#firstp").unbind({
|
||||
click:handler,
|
||||
mouseover:handler
|
||||
});
|
||||
|
||||
trigger();
|
||||
equals( clickCounter, 4, "bind(Object)" );
|
||||
equals( mouseoverCounter, 4, "bind(Object)" );
|
||||
});
|
||||
|
||||
test("bind(), iframes", function() {
|
||||
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
|
||||
var doc = jQuery("#loadediframe").contents();
|
||||
|
|
Loading…
Reference in a new issue