Landing the new expando management code. Completely overhauls how data is associated with elements.

Plugins will be most interested in:
- jQuery.data(elem) -> Unique ID for the element
- jQuery.data(elem, name) -> Named data store for the element
- jQuery.data(elem, name, value) -> Saves a value to the named data store
- jQuery.removeData(elem) -> Remove the expando and the complete data store
- jQuery.removeData(elem, name) -> Removes just this one named data store

jQuery's .remove() and .empty() automatically clean up after themselves. Once an element leaves a DOM document their events are no longer intact. Thus, statements like so:
{{{
  $("#foo").remove().appendTo("#bar");
}}}
should be written like so:
{{{
  $("#foo").appendTo("#bar");
}}}
in order to avoid losing the bound events.
This commit is contained in:
John Resig
2007-09-08 23:31:23 +00:00
parent 15a78f8fea
commit 3a4e1233aa
5 changed files with 108 additions and 47 deletions

View File

@@ -238,7 +238,7 @@ jQuery.fn = jQuery.prototype = {
var clone = ret.find("*").andSelf();
this.find("*").andSelf().each(function(i) {
var events = this.$events;
var events = jQuery.data(this, "events");
for ( var type in events )
for ( var handler in events[type] )
jQuery.event.add(clone[i], type, events[type][handler], events[type][handler].data);
@@ -411,6 +411,8 @@ jQuery.extend = jQuery.fn.extend = function() {
if ( target == prop[i] )
continue;
// Recurse if we're merging object values
if ( deep && typeof prop[i] == 'object' && target[i] )
jQuery.extend( target[i], prop[i] );
// Don't bring in undefined values
@@ -450,6 +452,58 @@ jQuery.extend({
globalEval: function( data ) {
data = jQuery.trim( data );
if ( data ) {
if ( window.execScript )
window.execScript( data );
else if ( jQuery.browser.safari )
// safari doesn't provide a synchronous global eval
window.setTimeout( data, 0 );
else
eval.call( window, data );
}
},
nodeName: function( elem, name ) {
return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
},
cache: {},
data: function( elem, name, data ) {
var id = elem[ expando ];
// 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 && !jQuery.cache[ id ] )
jQuery.cache[ id ] = {};
// Prevent overriding the named cache with undefined values
if ( data != undefined )
jQuery.cache[ id ][ name ] = data;
// Return the named cache data, or the ID for the element
return name ? jQuery.cache[ id ][ name ] : id;
},
removeData: function( elem, name ) {
var id = elem[ expando ];
// If we want to remove a specific section of the element's data
if ( name ) {
// Remove the section of cache data
delete jQuery.cache[ id ][ name ];
// If we've removed all the data, remove the element's cache
name = "";
for ( name in jQuery.cache[ id ] ) break;
if ( !name )
jQuery.removeData( elem );
// Otherwise, we want to remove all of the element's data
} else {
// Clean up the element expando
try {
delete elem[ expando ];
@@ -836,14 +890,16 @@ jQuery.extend({
// expando of getElementsByTagName
// Also, we need to make sure that the correct elements are being returned
// (IE returns comment nodes in a '*' query)
// (IE returns comment nodes in a '*' query)
if ( jQuery.browser.msie ) {
for ( var i = 0; second[i]; i++ )
if ( second[i].nodeType != 8 )
first.push(second[i]);
} else
if ( second[i].nodeType != 8 )
first.push(second[i]);
} else
for ( var i = 0; second[i]; i++ )
first.push(second[i]);
return first;
},
unique: function(first) {
@@ -851,8 +907,6 @@ jQuery.extend({
try {
for ( var i = 0, fl = first.length; i < fl; i++ ) {
try {
for ( var i = 0, fl = first.length; i < fl; i++ )
var id = jQuery.data(first[i]);
if ( !done[id] ) {
done[id] = true;
@@ -977,10 +1031,15 @@ jQuery.each( {
jQuery.each( {
removeAttr: function( key ) {
jQuery.attr( this, key, "" );
jQuery.attr( this, key, "" );
this.removeAttribute( key );
},
addClass: function(c){
jQuery.className.add(this,c);
},
removeClass: function(c){
jQuery.className.remove(this,c);
},
toggleClass: function( c ){
jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
},