aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoradamcoulombe <adamcoulombe187@hotmail.com>2013-01-03 17:05:00 -0500
committerDave Methvin <dave.methvin@gmail.com>2013-01-27 13:35:30 -0500
commit54fc5fdfa2e07fb08f2652f4b48d51ffc2999938 (patch)
tree35823e676f1c59a5448604bc8768efbf65c908b9
parent64b55f0b7912d96c3bdae8c771b4da99c96954fd (diff)
downloadjquery-54fc5fdfa2e07fb08f2652f4b48d51ffc2999938.tar.gz
jquery-54fc5fdfa2e07fb08f2652f4b48d51ffc2999938.zip
Fix #13150, .has() w/o args checks for any callbacks. Close gh-1111.
-rw-r--r--src/callbacks.js5
-rw-r--r--test/unit/callbacks.js47
2 files changed, 50 insertions, 2 deletions
diff --git a/src/callbacks.js b/src/callbacks.js
index 58f1402a8..2bb378047 100644
--- a/src/callbacks.js
+++ b/src/callbacks.js
@@ -137,9 +137,10 @@ jQuery.Callbacks = function( options ) {
}
return this;
},
- // Control if a given callback is in the list
+ // Check if a given callback is in the list.
+ // If no argument is given, return whether or not list has callbacks attached.
has: function( fn ) {
- return jQuery.inArray( fn, list ) > -1;
+ return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length );
},
// Remove all callbacks from the list
empty: function() {
diff --git a/test/unit/callbacks.js b/test/unit/callbacks.js
index bd61207ae..9482832db 100644
--- a/test/unit/callbacks.js
+++ b/test/unit/callbacks.js
@@ -269,6 +269,53 @@ test( "jQuery.Callbacks.remove - should remove all instances", function() {
}).remove( fn ).fire();
});
+test( "jQuery.Callbacks.has", function() {
+
+ expect( 13 );
+
+ var cb = jQuery.Callbacks();
+ function getA() {
+ return "A";
+ }
+ function getB() {
+ return "B";
+ }
+ function getC() {
+ return "C";
+ }
+ cb.add(getA, getB, getC);
+ strictEqual( cb.has(), true, "No arguments to .has() returns whether callback function(s) are attached or not" );
+ strictEqual( cb.has(getA), true, "Check if a specific callback function is in the Callbacks list" );
+
+ cb.remove(getB);
+ strictEqual( cb.has(getB), false, "Remove a specific callback function and make sure its no longer there" );
+ strictEqual( cb.has(getA), true, "Remove a specific callback function and make sure other callback function is still there" );
+
+ cb.empty();
+ strictEqual( cb.has(), false, "Empty list and make sure there are no callback function(s)" );
+ strictEqual( cb.has(getA), false, "Check for a specific function in an empty() list" );
+
+ cb.add(getA, getB, function(){
+ strictEqual( cb.has(), true, "Check if list has callback function(s) from within a callback function" );
+ strictEqual( cb.has(getA), true, "Check if list has a specific callback from within a callback function" );
+ }).fire();
+
+ strictEqual( cb.has(), true, "Callbacks list has callback function(s) after firing" );
+
+ cb.disable();
+ strictEqual( cb.has(), false, "disabled() list has no callback functions (returns false)" );
+ strictEqual( cb.has(getA), false, "Check for a specific function in a disabled() list" );
+
+ cb = jQuery.Callbacks("unique");
+ cb.add(getA);
+ cb.add(getA);
+ strictEqual( cb.has(), true, "Check if unique list has callback function(s) attached" );
+ cb.lock();
+ strictEqual( cb.has(), false, "locked() list is empty and returns false" );
+
+
+});
+
test( "jQuery.Callbacks() - adding a string doesn't cause a stack overflow", function() {
expect( 1 );