X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fdata.js;h=500f7ea43f0508c41410e28eff2b661b85dd6991;hb=70b9aed422c34047299fe6e8934902fae61f2a3e;hp=03116e4b10ed9223b4becec72b407bce03995ddd;hpb=991d039b62f5dfcb9e3d99fe28212a6874e8f5c7;p=jquery.git diff --git a/src/data.js b/src/data.js index 03116e4..500f7ea 100644 --- a/src/data.js +++ b/src/data.js @@ -1,31 +1,45 @@ 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 ] = {}; + // Handle the case where there's no name immediately + if ( !name && !id ) { + return null; + } - var thisCache = cache[ id ]; + // Compute a unique ID for the element + if ( !id ) { + id = ++uuid; + } + // 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 ( data !== undefined ) { + elem[ expando ] = id; + thisCache[ name ] = data; + } + + return name ? thisCache[ name ] : thisCache; }, removeData: function( elem, name ) { @@ -42,8 +56,9 @@ jQuery.extend({ delete thisCache[ name ]; // If we've removed all the data, remove the element's cache - if( jQuery.isEmptyObject(thisCache) ) + if ( jQuery.isEmptyObject(thisCache) ) { jQuery.removeData( elem ); + } } // Otherwise, we want to remove all of the element's data @@ -51,17 +66,19 @@ jQuery.extend({ // Clean up the element expando try { delete elem[ expando ]; - } catch(e){ + } catch( e ) { // IE has trouble directly removing the expando // but it's ok with using removeAttribute - if ( elem.removeAttribute ) + if ( elem.removeAttribute ) { elem.removeAttribute( expando ); + } } // Completely remove the data cache delete cache[ id ]; } }, + queue: function( elem, type, data ) { if( !elem ) return; @@ -99,7 +116,9 @@ jQuery.extend({ jQuery.fn.extend({ data: function( key, value ){ - if(typeof key === "undefined" && this.length) return jQuery.data(this[0], true); + if ( typeof key === "undefined" && this.length ) { + return jQuery.data( this[0] ); + } var parts = key.split("."); parts[1] = parts[1] ? "." + parts[1] : ""; @@ -148,4 +167,4 @@ jQuery.fn.extend({ clearQueue: function(type){ return this.queue( type || "fx", [] ); } -}); \ No newline at end of file +});