$.Callbacks.remove now accepts multiple parameters (Unit test amended). Changed the variable name of the main object from "object" to "self" as per rwaldron's suggestions.

This commit is contained in:
jaubourg 2011-05-24 21:16:51 +02:00
parent 8c39fc855e
commit 4dce543ee6
2 changed files with 35 additions and 30 deletions

View file

@ -70,7 +70,7 @@ jQuery.Callbacks = function( flags, filter ) {
firingLength, firingLength,
// Index of currently firing callback (modified by remove if needed) // Index of currently firing callback (modified by remove if needed)
firingIndex, firingIndex,
// Add a list of callbacks to the list // Add one or several callbacks to the list
add = function( args ) { add = function( args ) {
var i, var i,
length, length,
@ -87,9 +87,9 @@ jQuery.Callbacks = function( flags, filter ) {
// If we have to relocate, we remove the callback // If we have to relocate, we remove the callback
// if it already exists // if it already exists
if ( flags.relocate ) { if ( flags.relocate ) {
object.remove( elem ); self.remove( elem );
// Skip if we're in unique mode and callback is already in // Skip if we're in unique mode and callback is already in
} else if ( flags.unique && object.has( elem ) ) { } else if ( flags.unique && self.has( elem ) ) {
continue; continue;
} }
// Get the filtered function if needs be // Get the filtered function if needs be
@ -100,6 +100,7 @@ jQuery.Callbacks = function( flags, filter ) {
} }
} }
}, },
// Fire callbacks
fire = function( context, args ) { fire = function( context, args ) {
args = args || []; args = args || [];
memory = !flags.memory || [ context, args ]; memory = !flags.memory || [ context, args ];
@ -118,16 +119,17 @@ jQuery.Callbacks = function( flags, filter ) {
if ( !flags.once ) { if ( !flags.once ) {
if ( stack && stack.length ) { if ( stack && stack.length ) {
memory = stack.shift(); memory = stack.shift();
object.fireWith( memory[ 0 ], memory[ 1 ] ); self.fireWith( memory[ 0 ], memory[ 1 ] );
} }
} else if ( memory === true ) { } else if ( memory === true ) {
object.disable(); self.disable();
} else { } else {
list = []; list = [];
} }
} }
}, },
object = { // Actual Callbacks object
self = {
// Add a callback or a collection of callbacks to the list // Add a callback or a collection of callbacks to the list
add: function() { add: function() {
if ( list ) { if ( list ) {
@ -150,25 +152,30 @@ jQuery.Callbacks = function( flags, filter ) {
return this; return this;
}, },
// Remove a callback from the list // Remove a callback from the list
remove: function( fn ) { remove: function() {
if ( list ) { if ( list ) {
for ( var i = 0; i < list.length; i++ ) { var args = arguments,
if ( fn === list[ i ][ 0 ] ) { argIndex = 0,
// Handle firingIndex and firingLength argLength = args.length;
if ( firing ) { for ( ; argIndex < argLength ; argIndex++ ) {
if ( i <= firingLength ) { for ( var i = 0; i < list.length; i++ ) {
firingLength--; if ( args[ argIndex ] === list[ i ][ 0 ] ) {
if ( i <= firingIndex ) { // Handle firingIndex and firingLength
firingIndex--; if ( firing ) {
if ( i <= firingLength ) {
firingLength--;
if ( i <= firingIndex ) {
firingIndex--;
}
} }
} }
} // Remove the element
// Remove the element list.splice( i--, 1 );
list.splice( i--, 1 ); // If we have some unicity property then
// If we have some unicity property then // we only need to do this once
// we only need to do this once if ( flags.unique || flags.relocate ) {
if ( flags.unique || flags.relocate ) { break;
break; }
} }
} }
} }
@ -206,7 +213,7 @@ jQuery.Callbacks = function( flags, filter ) {
lock: function() { lock: function() {
stack = undefined; stack = undefined;
if ( !memory || memory === true ) { if ( !memory || memory === true ) {
object.disable(); self.disable();
} }
return this; return this;
}, },
@ -229,7 +236,7 @@ jQuery.Callbacks = function( flags, filter ) {
}, },
// Call all the callbacks with the given arguments // Call all the callbacks with the given arguments
fire: function() { fire: function() {
object.fireWith( this, arguments ); self.fireWith( this, arguments );
return this; return this;
}, },
// To know if the callbacks have already been called at least once // To know if the callbacks have already been called at least once
@ -238,7 +245,7 @@ jQuery.Callbacks = function( flags, filter ) {
} }
}; };
return object; return self;
}; };
})( jQuery ); })( jQuery );

View file

@ -98,12 +98,10 @@ jQuery.each( tests, function( flags, resultString ) {
// Basic binding, removing and firing // Basic binding, removing and firing
output = "X"; output = "X";
cblist = jQuery.Callbacks( flags ); cblist = jQuery.Callbacks( flags );
cblist.add( outputA ); cblist.add( outputA, outputB, outputC );
cblist.add( outputB ); cblist.remove( outputB, outputC );
cblist.add( outputC );
cblist.remove( outputB );
cblist.fire(); cblist.fire();
strictEqual( output, "XAC", "Basic binding, removing and firing" ); strictEqual( output, "XA", "Basic binding, removing and firing" );
// Empty // Empty
output = "X"; output = "X";