tags:

views:

38

answers:

2

How does jQuery's data function work? Is it more efficient than giving the dom elements a class or attribute? Where is the data stored?

Cheers.

PS: I once saw a link to a page that does method search for jquery, but I lost it :P

A: 

See - http://api.jquery.com/data/

Sniffer
Thanks, but I already referred to that before coming here ;P
Matrym
+1  A: 

I believe that what you are looking for is James Padolsey's jQuery SourceCode viewer

And this is the actual code from jQuery:

// The following elements throw uncatchable exceptions if you
    // attempt to add expando properties to them.
    noData: {
        "embed": true,
        "object": true,
        "applet": true
    },

    data: function( elem, name, data ) {
        if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
            return;
        }

        elem = elem == window ?
            windowData :
            elem;

        var id = elem[ expando ], cache = jQuery.cache, thisCache;

        if ( !id && typeof name === "string" && data === undefined ) {
            return null;
        }

        // 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 ( typeof name === "object" ) {
            elem[ expando ] = id;
            thisCache = cache[ id ] = jQuery.extend(true, {}, name);

        } else if ( !cache[ id ] ) {
            elem[ expando ] = id;
            cache[ id ] = {};
        }

        thisCache = cache[ id ];

        // Prevent overriding the named cache with undefined values
        if ( data !== undefined ) {
            thisCache[ name ] = data;
        }

        return typeof name === "string" ? thisCache[ name ] : thisCache;
    },

    removeData: function( elem, name ) {
        if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
            return;
        }

        elem = elem == window ?
            windowData :
            elem;

        var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];

        // If we want to remove a specific section of the element's data
        if ( name ) {
            if ( thisCache ) {
                // Remove the section of cache data
                delete thisCache[ name ];

                // If we've removed all the data, remove the element's cache
                if ( jQuery.isEmptyObject(thisCache) ) {
                    jQuery.removeData( elem );
                }
            }

        // Otherwise, we want to remove all of the element's data
        } else {
            if ( jQuery.support.deleteExpando ) {
                delete elem[ jQuery.expando ];

            } else if ( elem.removeAttribute ) {
                elem.removeAttribute( jQuery.expando );
            }

            // Completely remove the data cache
            delete cache[ id ];
        }
    }
Sean Vieira
Thanks. This is what I was looking for. Do you know anything about the efficiency of data, and jquery's cache? How does jquery cache work?
Matrym