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

@ -8,8 +8,8 @@ test("bind()", function() {
ok( event.data.foo == "bar", "bind() with data, Check value of passed data" );
};
$("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
ok( !$("#firstp").get(0).$events, "Event handler unbound when using data." );
ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." );
reset();
var handler = function(event, data) {
@ -108,11 +108,11 @@ test("unbind(event)", function() {
el.click(function() { return; });
el.unbind('change',function(){ return; });
for (var ret in el[0].$events['click']) break;
for (var ret in jQuery.data(el[0], "events")['click']) break;
ok( ret, "Extra handlers weren't accidentally removed." );
el.unbind('click');
ok( !el[0].$events, "Removed the events expando after all handlers are unbound." );
ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
});
test("trigger(event, [data], [fn])", function() {