]> source.dussan.org Git - jquery.git/commitdiff
Restore jQuery.access parameter mistakenly removed in 80d45a69
authorRichard Gibson <richard.gibson@gmail.com>
Tue, 6 Nov 2012 14:53:00 +0000 (09:53 -0500)
committerRichard Gibson <richard.gibson@gmail.com>
Tue, 6 Nov 2012 14:53:00 +0000 (09:53 -0500)
src/core.js
src/data.js
test/unit/data.js

index f34d72c25ab50a4c3cb4417f33aa875ce92e1e1e..53b844d59b11293f6798803af268977df76c9d85 100644 (file)
@@ -782,41 +782,44 @@ jQuery.extend({
 
        // Multifunctional method to get and set values of a collection
        // The value/s can optionally be executed if it's a function
-       access: function( elems, fn, key, value, chainable, emptyGet ) {
+       access: function( elems, fn, key, value, chainable, emptyGet, raw ) {
                var i = 0,
                        length = elems.length,
-                       bulk = key == null,
-                       exec = value !== undefined && jQuery.isFunction( value );
+                       bulk = key == null;
 
                // Sets many values
                if ( key && typeof key === "object" ) {
                        chainable = true;
                        for ( i in key ) {
-                               jQuery.access( elems, fn, i, key[i], true, emptyGet );
+                               jQuery.access( elems, fn, i, key[i], true, emptyGet, raw );
                        }
 
                // Sets one value
                } else if ( value !== undefined ) {
                        chainable = true;
 
-                       if ( bulk ) {
-                               // Bulk operations only iterate when executing function values
-                               if ( exec ) {
-                                       exec = fn;
-                                       fn = function( elem, key, value ) {
-                                               return exec.call( jQuery( elem ), value );
-                                       };
+                       if ( !jQuery.isFunction( value ) ) {
+                               raw = true;
+                       }
 
-                               // Otherwise they run against the entire set
-                               } else {
+                       if ( bulk ) {
+                               // Bulk operations run against the entire set
+                               if ( raw ) {
                                        fn.call( elems, value );
                                        fn = null;
+
+                               // ...except when executing function values
+                               } else {
+                                       bulk = fn;
+                                       fn = function( elem, key, value ) {
+                                               return bulk.call( jQuery( elem ), value );
+                                       };
                                }
                        }
 
                        if ( fn ) {
                                for ( ; i < length; i++ ) {
-                                       fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value );
+                                       fn( elems[i], key, raw ? value : value.call( elems[i], i, fn( elems[i], key ) ) );
                                }
                        }
                }
index af5f7c16f385c3556c963593492c8ef23b41ce6d..a61042709bfa745516bca292a5a83b4f78cb13b4 100644 (file)
@@ -277,7 +277,7 @@ jQuery.fn.extend({
                        this.each(function() {
                                jQuery.data( this, key, value );
                        });
-               }, null, value, arguments.length > 1, null, false );
+               }, null, value, arguments.length > 1, null, true );
        },
 
        removeData: function( key ) {
index f861ee4a2be3e0d27c3eacc4fe283e6764cd7dc8..c09149b65e34e73c309f86e1c0b9c9d7100e1c31 100644 (file)
@@ -174,40 +174,53 @@ test(".data()", function() {
        deepEqual( dataObj, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (#7524)" );
 });
 
-test(".data(String) and .data(String, Object)", function() {
-       expect( 12 );
+var testDataTypes = function( $obj ) {
+       jQuery.each({
+               "null": null,
+               "true": true,
+               "false": false,
+               "zero": 0,
+               "one": 1,
+               "empty string": "",
+               "empty array": [],
+               "array": [1],
+               "empty object": {},
+               "object": { foo: "bar" },
+               "date": new Date(),
+               "regex": /test/,
+               "function": function() {}
+       }, function( type, value ) {
+               strictEqual( $obj.data( "test", value ).data("test"), value, "Data set to " + type );
+       });
+};
+
+test("jQuery(Element).data(String, Object).data(String)", function() {
+       expect( 18 );
        var parent = jQuery("<div><div></div></div>"),
                div = parent.children();
 
-       ok( div.data("test") === undefined, "Check for no data exists" );
-
-       div.data("test", "success");
-       equal( div.data("test"), "success", "Check for added data" );
-
-       div.data("test", "overwritten");
-       equal( div.data("test"), "overwritten", "Check for overwritten data" );
-
-       equal( div.data("test", undefined).data("test"), "overwritten", "Check that .data('key',undefined) does nothing but is chainable (#5571)");
+       strictEqual( div.data("test"), undefined, "No data exists initially" );
+       strictEqual( div.data("test", "success").data("test"), "success", "Data added" );
+       strictEqual( div.data("test", "overwritten").data("test"), "overwritten", "Data overwritten" );
+       strictEqual( div.data("test", undefined).data("test"), "overwritten", ".data(key,undefined) does nothing but is chainable (#5571)");
+       strictEqual( div.data("notexist"), undefined, "No data exists for unset key" );
+       testDataTypes( div );
 
-       div.data("test", null);
-       ok( div.data("test") === null, "Check for null data");
+       parent.remove();
+});
 
-       ok( div.data("notexist") === undefined, "Check for no data exists" );
+test("jQuery(plain Object).data(String, Object).data(String)", function() {
+       expect( 16 );
 
        // #3748
-       var $elem = jQuery({exists:true});
-       equal( $elem.data("nothing"), undefined, "Non-existent data returns undefined");
-       equal( $elem.data("null", null).data("null"), null, "null's are preserved");
-       equal( $elem.data("emptyString", "").data("emptyString"), "", "Empty strings are preserved");
-       equal( $elem.data("false", false).data("false"), false, "false's are preserved");
-       equal( $elem.data("exists"), undefined, "Existing data is not returned" );
+       var $obj = jQuery({ exists: true });
+       strictEqual( $obj.data("nothing"), undefined, "Non-existent data returns undefined");
+       strictEqual( $obj.data("exists"), undefined, "Object properties are not returned as data" );
+       testDataTypes( $obj );
 
        // Clean up
-       $elem.removeData();
-       deepEqual( $elem[0], {exists:true}, "removeData does not clear the object" );
-
-       // manually clean up detached elements
-       parent.remove();
+       $obj.removeData();
+       deepEqual( $obj[0], { exists: true }, "removeData does not clear the object" );
 });
 
 test("data-* attributes", function() {