X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fdata.js;h=c18e73cfd7aacc6febc3deed2f233644bca55e26;hb=c4f144eeffd94c745839b0ced2de9c62cfa9f075;hp=03116e4b10ed9223b4becec72b407bce03995ddd;hpb=991d039b62f5dfcb9e3d99fe28212a6874e8f5c7;p=jquery.git diff --git a/src/data.js b/src/data.js index 03116e4..c18e73c 100644 --- a/src/data.js +++ b/src/data.js @@ -1,31 +1,38 @@ var expando = "jQuery" + now(), uuid = 0, windowData = {}; +var emptyObject = {}; jQuery.extend({ cache: {}, + + expando:expando, data: function( elem, name, data ) { elem = elem == window ? windowData : elem; - var id = elem[ expando ], cache = jQuery.cache; + var id = elem[ expando ], cache = jQuery.cache, thisCache; // Compute a unique ID for the element if(!id) id = elem[ expando ] = ++uuid; - // Only generate the data cache if we're - // trying to access or manipulate it - if ( name && !cache[ id ] ) - cache[ id ] = {}; - - var thisCache = cache[ id ]; + // Handle the case where there's no name immediately + if ( !name ) { return id; } + // Avoid generating a new cache unless none exists and we + // want to manipulate it. + if( cache[ id ] ) + thisCache = cache[ id ]; + else if( typeof data === "undefined" ) + thisCache = emptyObject; + else + thisCache = cache[ id ] = {}; + // Prevent overriding the named cache with undefined values if ( data !== undefined ) thisCache[ name ] = data; - if(name === true) return thisCache - else if(name) return thisCache[name] - else return id + if(name === true) return thisCache; + else return thisCache[name]; }, removeData: function( elem, name ) {