Skip to content

Commit 4dce543

Browse files
committed
$.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.
1 parent 8c39fc8 commit 4dce543

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/callbacks.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jQuery.Callbacks = function( flags, filter ) {
7070
firingLength,
7171
// Index of currently firing callback (modified by remove if needed)
7272
firingIndex,
73-
// Add a list of callbacks to the list
73+
// Add one or several callbacks to the list
7474
add = function( args ) {
7575
var i,
7676
length,
@@ -87,9 +87,9 @@ jQuery.Callbacks = function( flags, filter ) {
8787
// If we have to relocate, we remove the callback
8888
// if it already exists
8989
if ( flags.relocate ) {
90-
object.remove( elem );
90+
self.remove( elem );
9191
// Skip if we're in unique mode and callback is already in
92-
} else if ( flags.unique && object.has( elem ) ) {
92+
} else if ( flags.unique && self.has( elem ) ) {
9393
continue;
9494
}
9595
// Get the filtered function if needs be
@@ -100,6 +100,7 @@ jQuery.Callbacks = function( flags, filter ) {
100100
}
101101
}
102102
},
103+
// Fire callbacks
103104
fire = function( context, args ) {
104105
args = args || [];
105106
memory = !flags.memory || [ context, args ];
@@ -118,16 +119,17 @@ jQuery.Callbacks = function( flags, filter ) {
118119
if ( !flags.once ) {
119120
if ( stack && stack.length ) {
120121
memory = stack.shift();
121-
object.fireWith( memory[ 0 ], memory[ 1 ] );
122+
self.fireWith( memory[ 0 ], memory[ 1 ] );
122123
}
123124
} else if ( memory === true ) {
124-
object.disable();
125+
self.disable();
125126
} else {
126127
list = [];
127128
}
128129
}
129130
},
130-
object = {
131+
// Actual Callbacks object
132+
self = {
131133
// Add a callback or a collection of callbacks to the list
132134
add: function() {
133135
if ( list ) {
@@ -150,25 +152,30 @@ jQuery.Callbacks = function( flags, filter ) {
150152
return this;
151153
},
152154
// Remove a callback from the list
153-
remove: function( fn ) {
155+
remove: function() {
154156
if ( list ) {
155-
for ( var i = 0; i < list.length; i++ ) {
156-
if ( fn === list[ i ][ 0 ] ) {
157-
// Handle firingIndex and firingLength
158-
if ( firing ) {
159-
if ( i <= firingLength ) {
160-
firingLength--;
161-
if ( i <= firingIndex ) {
162-
firingIndex--;
157+
var args = arguments,
158+
argIndex = 0,
159+
argLength = args.length;
160+
for ( ; argIndex < argLength ; argIndex++ ) {
161+
for ( var i = 0; i < list.length; i++ ) {
162+
if ( args[ argIndex ] === list[ i ][ 0 ] ) {
163+
// Handle firingIndex and firingLength
164+
if ( firing ) {
165+
if ( i <= firingLength ) {
166+
firingLength--;
167+
if ( i <= firingIndex ) {
168+
firingIndex--;
169+
}
163170
}
164171
}
165-
}
166-
// Remove the element
167-
list.splice( i--, 1 );
168-
// If we have some unicity property then
169-
// we only need to do this once
170-
if ( flags.unique || flags.relocate ) {
171-
break;
172+
// Remove the element
173+
list.splice( i--, 1 );
174+
// If we have some unicity property then
175+
// we only need to do this once
176+
if ( flags.unique || flags.relocate ) {
177+
break;
178+
}
172179
}
173180
}
174181
}
@@ -206,7 +213,7 @@ jQuery.Callbacks = function( flags, filter ) {
206213
lock: function() {
207214
stack = undefined;
208215
if ( !memory || memory === true ) {
209-
object.disable();
216+
self.disable();
210217
}
211218
return this;
212219
},
@@ -229,7 +236,7 @@ jQuery.Callbacks = function( flags, filter ) {
229236
},
230237
// Call all the callbacks with the given arguments
231238
fire: function() {
232-
object.fireWith( this, arguments );
239+
self.fireWith( this, arguments );
233240
return this;
234241
},
235242
// To know if the callbacks have already been called at least once
@@ -238,7 +245,7 @@ jQuery.Callbacks = function( flags, filter ) {
238245
}
239246
};
240247

241-
return object;
248+
return self;
242249
};
243250

244251
})( jQuery );

test/unit/callbacks.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ jQuery.each( tests, function( flags, resultString ) {
9898
// Basic binding, removing and firing
9999
output = "X";
100100
cblist = jQuery.Callbacks( flags );
101-
cblist.add( outputA );
102-
cblist.add( outputB );
103-
cblist.add( outputC );
104-
cblist.remove( outputB );
101+
cblist.add( outputA, outputB, outputC );
102+
cblist.remove( outputB, outputC );
105103
cblist.fire();
106-
strictEqual( output, "XAC", "Basic binding, removing and firing" );
104+
strictEqual( output, "XA", "Basic binding, removing and firing" );
107105

108106
// Empty
109107
output = "X";

0 commit comments

Comments
 (0)