Restore jQuery.access parameter mistakenly removed in 80d45a69

This commit is contained in:
Richard Gibson 2012-11-06 09:53:00 -05:00
parent 81b094b2c1
commit 0ee9415902
3 changed files with 56 additions and 40 deletions

View File

@ -782,41 +782,44 @@ jQuery.extend({
// Multifunctional method to get and set values of a collection // Multifunctional method to get and set values of a collection
// The value/s can optionally be executed if it's a function // 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, var i = 0,
length = elems.length, length = elems.length,
bulk = key == null, bulk = key == null;
exec = value !== undefined && jQuery.isFunction( value );
// Sets many values // Sets many values
if ( key && typeof key === "object" ) { if ( key && typeof key === "object" ) {
chainable = true; chainable = true;
for ( i in key ) { 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 // Sets one value
} else if ( value !== undefined ) { } else if ( value !== undefined ) {
chainable = true; chainable = true;
if ( bulk ) { if ( !jQuery.isFunction( value ) ) {
// Bulk operations only iterate when executing function values raw = true;
if ( exec ) { }
exec = fn;
fn = function( elem, key, value ) {
return exec.call( jQuery( elem ), value );
};
// Otherwise they run against the entire set if ( bulk ) {
} else { // Bulk operations run against the entire set
if ( raw ) {
fn.call( elems, value ); fn.call( elems, value );
fn = null; fn = null;
// ...except when executing function values
} else {
bulk = fn;
fn = function( elem, key, value ) {
return bulk.call( jQuery( elem ), value );
};
} }
} }
if ( fn ) { if ( fn ) {
for ( ; i < length; i++ ) { 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 ) ) );
} }
} }
} }

View File

@ -277,7 +277,7 @@ jQuery.fn.extend({
this.each(function() { this.each(function() {
jQuery.data( this, key, value ); jQuery.data( this, key, value );
}); });
}, null, value, arguments.length > 1, null, false ); }, null, value, arguments.length > 1, null, true );
}, },
removeData: function( key ) { removeData: function( key ) {

View File

@ -174,40 +174,53 @@ test(".data()", function() {
deepEqual( dataObj, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (#7524)" ); deepEqual( dataObj, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (#7524)" );
}); });
test(".data(String) and .data(String, Object)", function() { var testDataTypes = function( $obj ) {
expect( 12 ); 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>"), var parent = jQuery("<div><div></div></div>"),
div = parent.children(); div = parent.children();
ok( div.data("test") === undefined, "Check for no data exists" ); 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", "success"); parent.remove();
equal( div.data("test"), "success", "Check for added data" ); });
div.data("test", "overwritten"); test("jQuery(plain Object).data(String, Object).data(String)", function() {
equal( div.data("test"), "overwritten", "Check for overwritten data" ); expect( 16 );
equal( div.data("test", undefined).data("test"), "overwritten", "Check that .data('key',undefined) does nothing but is chainable (#5571)");
div.data("test", null);
ok( div.data("test") === null, "Check for null data");
ok( div.data("notexist") === undefined, "Check for no data exists" );
// #3748 // #3748
var $elem = jQuery({exists:true}); var $obj = jQuery({ exists: true });
equal( $elem.data("nothing"), undefined, "Non-existent data returns undefined"); strictEqual( $obj.data("nothing"), undefined, "Non-existent data returns undefined");
equal( $elem.data("null", null).data("null"), null, "null's are preserved"); strictEqual( $obj.data("exists"), undefined, "Object properties are not returned as data" );
equal( $elem.data("emptyString", "").data("emptyString"), "", "Empty strings are preserved"); testDataTypes( $obj );
equal( $elem.data("false", false).data("false"), false, "false's are preserved");
equal( $elem.data("exists"), undefined, "Existing data is not returned" );
// Clean up // Clean up
$elem.removeData(); $obj.removeData();
deepEqual( $elem[0], {exists:true}, "removeData does not clear the object" ); deepEqual( $obj[0], { exists: true }, "removeData does not clear the object" );
// manually clean up detached elements
parent.remove();
}); });
test("data-* attributes", function() { test("data-* attributes", function() {