Refactor jQuery.data a bit to reduce property lookups
[jquery.git] / src / data.js
index 8c79c21..9cc76c6 100644 (file)
@@ -8,25 +8,24 @@ jQuery.extend({
                        windowData :\r
                        elem;\r
 \r
-               var id = elem[ expando ];\r
+               var id = elem[ expando ], cache = jQuery.cache;\r
 \r
                // Compute a unique ID for the element\r
-               if ( !id )\r
-                       id = elem[ expando ] = ++uuid;\r
+               if(!id) id = elem[ expando ] = ++uuid;\r
 \r
                // Only generate the data cache if we're\r
                // trying to access or manipulate it\r
-               if ( name && !jQuery.cache[ id ] )\r
-                       jQuery.cache[ id ] = {};\r
+               if ( name && !cache[ id ] )\r
+                       cache[ id ] = {};\r
+\r
+               var thisCache = cache[ id ];\r
 \r
                // Prevent overriding the named cache with undefined values\r
-               if ( data !== undefined )\r
-                       jQuery.cache[ id ][ name ] = data;\r
+               if ( data !== undefined ) thisCache[ name ] = data;\r
 \r
-               // Return the named cache data, or the ID for the element\r
-               return name ?\r
-                       jQuery.cache[ id ][ name ] :\r
-                       id;\r
+               if(name === true) return thisCache\r
+               else if(name) return thisCache[name]\r
+               else return id\r
        },\r
 \r
        removeData: function( elem, name ) {\r
@@ -34,21 +33,16 @@ jQuery.extend({
                        windowData :\r
                        elem;\r
 \r
-               var id = elem[ expando ];\r
+               var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];\r
 \r
                // If we want to remove a specific section of the element's data\r
                if ( name ) {\r
-                       if ( jQuery.cache[ id ] ) {\r
+                       if ( thisCache ) {\r
                                // Remove the section of cache data\r
-                               delete jQuery.cache[ id ][ name ];\r
+                               delete thisCache[ name ];\r
 \r
                                // If we've removed all the data, remove the element's cache\r
-                               name = "";\r
-\r
-                               for ( name in jQuery.cache[ id ] )\r
-                                       break;\r
-\r
-                               if ( !name )\r
+                               if( jQuery.isEmptyObject(thisCache) )\r
                                        jQuery.removeData( elem );\r
                        }\r
 \r
@@ -65,7 +59,7 @@ jQuery.extend({
                        }\r
 \r
                        // Completely remove the data cache\r
-                       delete jQuery.cache[ id ];\r
+                       delete cache[ id ];\r
                }\r
        },\r
        queue: function( elem, type, data ) {\r
@@ -93,11 +87,13 @@ jQuery.extend({
 \r
                if( fn !== undefined )\r
                        fn.call(elem, function() { jQuery(elem).dequeue(type); });\r
-       },\r
+       }\r
 });\r
 \r
 jQuery.fn.extend({\r
        data: function( key, value ){\r
+               if(typeof key === "undefined" && this.length) return jQuery.data(this[0], true);\r
+\r
                var parts = key.split(".");\r
                parts[1] = parts[1] ? "." + parts[1] : "";\r
 \r
@@ -130,11 +126,11 @@ jQuery.fn.extend({
                if ( data === undefined )\r
                        return jQuery.queue( this[0], type );\r
 \r
-               return this.each(function(){\r
+               return this.each(function(i, elem){\r
                        var queue = jQuery.queue( this, type, data );\r
 \r
-                        if( type == "fx" && queue.length == 1 )\r
-                               queue[0].call(this);\r
+                       if( type == "fx" && queue.length == 1 )\r
+                               queue[0].call(this, function() { jQuery(elem).dequeue(type); });\r
                });\r
        },\r
        dequeue: function(type){\r
@@ -143,6 +139,6 @@ jQuery.fn.extend({
                });\r
        },\r
        clearQueue: function(type){\r
-               return this.queue( type, [] );\r
+               return this.queue( type || "fx", [] );\r
        }\r
 });
\ No newline at end of file