combined jQuery.bind() and jQuery.one() to make them more DRY
This commit is contained in:
parent
0ab118ae0d
commit
1bac61655b
42
src/event.js
42
src/event.js
|
@ -697,13 +697,12 @@ jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jQuery.fn.extend({
|
jQuery.each(["bind", "one"], function(i, name) {
|
||||||
// TODO: make bind(), unbind() and one() DRY!
|
jQuery.fn[ name ] = function( type, data, fn, thisObject ) {
|
||||||
bind: function( type, data, fn, thisObject ) {
|
|
||||||
// Handle object literals
|
// Handle object literals
|
||||||
if ( typeof type === "object" ) {
|
if ( typeof type === "object" ) {
|
||||||
for ( var key in type ) {
|
for ( var key in type ) {
|
||||||
this.bind(key, data, type[key], fn);
|
this[ name ](key, data, type[key], fn);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -714,35 +713,17 @@ jQuery.fn.extend({
|
||||||
data = undefined;
|
data = undefined;
|
||||||
}
|
}
|
||||||
fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
|
fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
|
||||||
return type === "unload" ? this.one(type, data, fn, thisObject) : this.each(function() {
|
var handler = name == "one" ? jQuery.event.proxy( fn, function( event ) {
|
||||||
jQuery.event.add( this, type, fn, data );
|
jQuery( this ).unbind( event, handler );
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
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;
|
|
||||||
data = undefined;
|
|
||||||
}
|
|
||||||
fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
|
|
||||||
var one = jQuery.event.proxy( fn, function( event ) {
|
|
||||||
jQuery( this ).unbind( event, one );
|
|
||||||
return fn.apply( this, arguments );
|
return fn.apply( this, arguments );
|
||||||
|
}) : fn;
|
||||||
|
return type === "unload" ? this.one(type, data, handler, thisObject) : this.each(function() {
|
||||||
|
jQuery.event.add( this, type, handler, data );
|
||||||
});
|
});
|
||||||
return this.each(function() {
|
};
|
||||||
jQuery.event.add( this, type, one, data );
|
});
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
|
jQuery.fn.extend({
|
||||||
unbind: function( type, fn ) {
|
unbind: function( type, fn ) {
|
||||||
// Handle object literals
|
// Handle object literals
|
||||||
if ( typeof type === "object" && !type.preventDefault ) {
|
if ( typeof type === "object" && !type.preventDefault ) {
|
||||||
|
@ -756,7 +737,6 @@ jQuery.fn.extend({
|
||||||
jQuery.event.remove( this, type, fn );
|
jQuery.event.remove( this, type, fn );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
trigger: function( type, data ) {
|
trigger: function( type, data ) {
|
||||||
return this.each(function() {
|
return this.each(function() {
|
||||||
jQuery.event.trigger( type, data, this );
|
jQuery.event.trigger( type, data, this );
|
||||||
|
|
Loading…
Reference in a new issue