]> source.dussan.org Git - jquery.git/commitdiff
Added support for breaking in an object loop (Bug #2111).
authorJohn Resig <jeresig@gmail.com>
Mon, 14 Jan 2008 20:06:34 +0000 (20:06 +0000)
committerJohn Resig <jeresig@gmail.com>
Mon, 14 Jan 2008 20:06:34 +0000 (20:06 +0000)
src/core.js
test/unit/core.js

index bba9883f589a429245455beb03dcb2d1a4404648..8bec2c74f2077bde79f9df1ae6d064c235434cd2 100644 (file)
@@ -707,20 +707,22 @@ jQuery.extend({
        // args is for internal usage only\r
        each: function( object, callback, args ) {\r
                if ( args ) {\r
-                       if ( object.length == undefined )\r
+                       if ( object.length == undefined ) {\r
                                for ( var name in object )\r
-                                       callback.apply( object[ name ], args );\r
-                       else\r
+                                       if ( callback.apply( object[ name ], args ) === false )\r
+                                               break;\r
+                       } else\r
                                for ( var i = 0, length = object.length; i < length; i++ )\r
                                        if ( callback.apply( object[ i ], args ) === false )\r
                                                break;\r
 \r
                // A special, fast, case for the most common use of each\r
                } else {\r
-                       if ( object.length == undefined )\r
+                       if ( object.length == undefined ) {\r
                                for ( var name in object )\r
-                                       callback.call( object[ name ], name, object[ name ] );\r
-                       else\r
+                                       if ( callback.call( object[ name ], name, object[ name ] ) === false )\r
+                                               break;\r
+                       } else\r
                                for ( var i = 0, length = object.length, value = object[0]; \r
                                        i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}\r
                }\r
index 72130102a369eaf2c81dca3a629959bad2025f4d..b2cf8f137e8a1cf97373e1359b92e635c06ade81 100644 (file)
@@ -1314,7 +1314,7 @@ test("text(String)", function() {
 });
 
 test("$.each(Object,Function)", function() {
-       expect(8);
+       expect(12);
        $.each( [0,1,2], function(i, n){
                ok( i == n, "Check array iteration" );
        });
@@ -1326,6 +1326,19 @@ test("$.each(Object,Function)", function() {
        $.each( { name: "name", lang: "lang" }, function(i, n){
                ok( i == n, "Check object iteration" );
        });
+
+        var total = 0;
+        jQuery.each([1,2,3], function(i,v){ total += v; });
+        ok( total == 6, "Looping over an array" );
+        total = 0;
+        jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
+        ok( total == 3, "Looping over an array, with break" );
+        total = 0;
+        jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
+        ok( total == 6, "Looping over an object" );
+        total = 0;
+        jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
+        ok( total == 3, "Looping over an object, with break" );
 });
 
 test("$.prop", function() {