Ticket #6808. Changes data() so on plain objects, it uses a function to contain the cache ID to avoid it being JSON serialized.
This commit is contained in:
parent
ef9fb80cab
commit
9faab0b74f
2 changed files with 29 additions and 7 deletions
29
src/data.js
29
src/data.js
|
@ -46,13 +46,30 @@ jQuery.extend({
|
||||||
// Avoid generating a new cache unless none exists and we
|
// Avoid generating a new cache unless none exists and we
|
||||||
// want to manipulate it.
|
// want to manipulate it.
|
||||||
if ( typeof name === "object" ) {
|
if ( typeof name === "object" ) {
|
||||||
cache[ id ] = jQuery.extend(true, {}, name);
|
if ( isNode ) {
|
||||||
|
cache[ id ] = jQuery.extend(true, {}, name);
|
||||||
|
} else {
|
||||||
|
cache[ id ] = function() {
|
||||||
|
return jQuery.extend(true, {}, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else if ( !cache[ id ] ) {
|
} else if ( !cache[ id ] ) {
|
||||||
cache[ id ] = {};
|
if ( isNode ) {
|
||||||
|
cache[ id ] = {};
|
||||||
|
} else {
|
||||||
|
var store = {};
|
||||||
|
cache[ id ] = function() {
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thisCache = cache[ id ];
|
thisCache = cache[ id ];
|
||||||
|
if ( !isNode ) {
|
||||||
|
thisCache = thisCache();
|
||||||
|
}
|
||||||
|
|
||||||
// Prevent overriding the named cache with undefined values
|
// Prevent overriding the named cache with undefined values
|
||||||
if ( data !== undefined ) {
|
if ( data !== undefined ) {
|
||||||
|
@ -71,8 +88,12 @@ jQuery.extend({
|
||||||
windowData :
|
windowData :
|
||||||
elem;
|
elem;
|
||||||
|
|
||||||
var id = elem[ jQuery.expando ], cache = jQuery.cache,
|
var isNode = elem.nodeType,
|
||||||
isNode = elem.nodeType, thisCache = isNode ? cache[ id ] : id;
|
id = elem[ jQuery.expando ], cache = jQuery.cache;
|
||||||
|
if ( id && !isNode ) {
|
||||||
|
id = id();
|
||||||
|
}
|
||||||
|
var thisCache = cache[ id ];
|
||||||
|
|
||||||
// If we want to remove a specific section of the element's data
|
// If we want to remove a specific section of the element's data
|
||||||
if ( name ) {
|
if ( name ) {
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
module("data");
|
module("data");
|
||||||
|
|
||||||
test("expando", function(){
|
test("expando", function(){
|
||||||
expect(6);
|
expect(7);
|
||||||
|
|
||||||
equals("expando" in jQuery, true, "jQuery is exposing the expando");
|
equals("expando" in jQuery, true, "jQuery is exposing the expando");
|
||||||
|
|
||||||
var obj = {};
|
var obj = {};
|
||||||
jQuery.data(obj);
|
jQuery.data(obj);
|
||||||
equals( jQuery.expando in obj, true, "jQuery.data adds an expando to the object" );
|
equals( jQuery.expando in obj, true, "jQuery.data adds an expando to the object" );
|
||||||
|
equals( typeof obj[jQuery.expando], "function", "jQuery.data adds an expando to the object as a function" );
|
||||||
|
|
||||||
obj = {};
|
obj = {};
|
||||||
jQuery.data(obj, 'test');
|
jQuery.data(obj, 'test');
|
||||||
|
@ -17,7 +18,7 @@ test("expando", function(){
|
||||||
jQuery.data(obj, "foo", "bar");
|
jQuery.data(obj, "foo", "bar");
|
||||||
equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
|
equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
|
||||||
|
|
||||||
var id = obj[jQuery.expando];
|
var id = obj[jQuery.expando]();
|
||||||
equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
|
equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
|
||||||
|
|
||||||
equals( id.foo, "bar", "jQuery.data worked correctly" );
|
equals( id.foo, "bar", "jQuery.data worked correctly" );
|
||||||
|
@ -54,7 +55,7 @@ test("jQuery.data", function() {
|
||||||
jQuery.data( obj, "prop", true );
|
jQuery.data( obj, "prop", true );
|
||||||
|
|
||||||
ok( obj[ jQuery.expando ], "Data is being stored on the object." );
|
ok( obj[ jQuery.expando ], "Data is being stored on the object." );
|
||||||
ok( obj[ jQuery.expando ].prop, "Data is being stored on the object." );
|
ok( obj[ jQuery.expando ]().prop, "Data is being stored on the object." );
|
||||||
|
|
||||||
equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
|
equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue