Skip to content

Commit 6be9747

Browse files
author
InfinitiesLoop
committed
Ticket #6808. Changes data() so on plain objects, it uses a function to contain the cache ID to avoid it being JSON serialized.
1 parent 2f1aea7 commit 6be9747

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/data.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,30 @@ jQuery.extend({
4646
// Avoid generating a new cache unless none exists and we
4747
// want to manipulate it.
4848
if ( typeof name === "object" ) {
49-
cache[ id ] = jQuery.extend(true, {}, name);
49+
if ( isNode ) {
50+
cache[ id ] = jQuery.extend(true, {}, name);
51+
} else {
52+
cache[ id ] = function() {
53+
return jQuery.extend(true, {}, name);
54+
}
55+
}
5056

5157
} else if ( !cache[ id ] ) {
52-
cache[ id ] = {};
58+
if ( isNode ) {
59+
cache[ id ] = {};
60+
} else {
61+
var store = {};
62+
cache[ id ] = function() {
63+
return store;
64+
}
65+
}
66+
5367
}
5468

5569
thisCache = cache[ id ];
70+
if ( !isNode ) {
71+
thisCache = thisCache();
72+
}
5673

5774
// Prevent overriding the named cache with undefined values
5875
if ( data !== undefined ) {
@@ -71,8 +88,12 @@ jQuery.extend({
7188
windowData :
7289
elem;
7390

74-
var id = elem[ jQuery.expando ], cache = jQuery.cache,
75-
isNode = elem.nodeType, thisCache = isNode ? cache[ id ] : id;
91+
var isNode = elem.nodeType,
92+
id = elem[ jQuery.expando ], cache = jQuery.cache;
93+
if ( id && !isNode ) {
94+
id = id();
95+
}
96+
var thisCache = cache[ id ];
7697

7798
// If we want to remove a specific section of the element's data
7899
if ( name ) {

test/unit/data.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module("data");
22

33
test("expando", function(){
4-
expect(6);
4+
expect(7);
55

66
equals("expando" in jQuery, true, "jQuery is exposing the expando");
77

88
var obj = {};
99
jQuery.data(obj);
1010
equals( jQuery.expando in obj, true, "jQuery.data adds an expando to the object" );
11+
equals( typeof obj[jQuery.expando], "function", "jQuery.data adds an expando to the object as a function" );
1112

1213
obj = {};
1314
jQuery.data(obj, 'test');
@@ -17,7 +18,7 @@ test("expando", function(){
1718
jQuery.data(obj, "foo", "bar");
1819
equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
1920

20-
var id = obj[jQuery.expando];
21+
var id = obj[jQuery.expando]();
2122
equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
2223

2324
equals( id.foo, "bar", "jQuery.data worked correctly" );
@@ -54,7 +55,7 @@ test("jQuery.data", function() {
5455
jQuery.data( obj, "prop", true );
5556

5657
ok( obj[ jQuery.expando ], "Data is being stored on the object." );
57-
ok( obj[ jQuery.expando ].prop, "Data is being stored on the object." );
58+
ok( obj[ jQuery.expando ]().prop, "Data is being stored on the object." );
5859

5960
equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
6061
});

0 commit comments

Comments
 (0)