Added $().data(), $().removeData(), and .bind("click!"). .data() and .removeData() handle namespaced data, .data() triggers a "set-KEY" event on all modified elements, and .bind("click!") only triggers a click (and no namespaced events).

This commit is contained in:
John Resig 2008-02-03 04:05:24 +00:00
parent 25c76232c6
commit 77da94552e
3 changed files with 79 additions and 1 deletions

View file

@ -477,6 +477,26 @@ jQuery.fn = jQuery.prototype = {
andSelf: function() { andSelf: function() {
return this.add( this.prevObject ); return this.add( this.prevObject );
}, },
data: function( key, value ){
if ( value == null ) {
if ( this.length ) {
var data = jQuery.data( this[0], key );
return data == null ?
jQuery.data( this[0], key.split(".")[0] ) :
data;
}
} else
return this.trigger("set-" + key + "!", [value]).each(function(){
jQuery.data( this, key, value );
});
},
removeData: function( key ){
return this.each(function(){
jQuery.removeData( this, key );
});
},
domManip: function( args, table, reverse, callback ) { domManip: function( args, table, reverse, callback ) {
var clone = this.length > 1, elems; var clone = this.length > 1, elems;

View file

@ -169,6 +169,11 @@ jQuery.event = {
// Clone the incoming data, if any // Clone the incoming data, if any
data = jQuery.makeArray(data || []); data = jQuery.makeArray(data || []);
if ( type.indexOf("!") >= 0 ) {
type = type.slice(0, -1);
var exclusive = true;
}
// Handle a global trigger // Handle a global trigger
if ( !elem ) { if ( !elem ) {
// Only trigger if we've ever bound an event for it // Only trigger if we've ever bound an event for it
@ -191,6 +196,8 @@ jQuery.event = {
// Enforce the right trigger type // Enforce the right trigger type
data[0].type = type; data[0].type = type;
if ( exclusive )
data[0].exclusive = true;
// Trigger the event // Trigger the event
if ( jQuery.isFunction( jQuery.data(elem, "handle") ) ) if ( jQuery.isFunction( jQuery.data(elem, "handle") ) )
@ -250,7 +257,7 @@ jQuery.event = {
args[0].data = handler.data; args[0].data = handler.data;
// Filter the functions by class // Filter the functions by class
if ( !parts[1] || handler.type == parts[1] ) { if ( !parts[1] && !event.exclusive || handler.type == parts[1] ) {
var ret = handler.apply( this, args ); var ret = handler.apply( this, args );
if ( val !== false ) if ( val !== false )

View file

@ -1397,6 +1397,36 @@ test("$.data", function() {
ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" ); ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" );
}); });
test(".data()", function() {
expect(11);
var div = $("#foo");
ok( div.data("test") == undefined, "Check for no data exists" );
div.data("test", "success");
ok( div.data("test") == "success", "Check for added data" );
div.data("test", "overwritten");
ok( div.data("test") == "overwritten", "Check for overwritten data" );
var hits = 0;
div
.bind("set-test",function(){ hits += 1; })
.bind("set-test.foo",function(){ hits += 2; })
div.data("test.foo", "foodata");
ok( div.data("test") == "overwritten", "Check for original data" );
ok( div.data("test.foo") == "foodata", "Check for namespaced data" );
ok( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
ok( hits == 2, "Check triggered functions" );
hits = 0;
div.data("test", "overwritten2");
ok( div.data("test") == "overwritten2", "Check for original data" );
ok( div.data("test.foo") == "foodata", "Check for namespaced data" );
ok( div.data("test.bar") == "overwritten2", "Check for unmatched namespace" );
ok( hits == 1, "Check triggered functions" );
});
test("$.removeData", function() { test("$.removeData", function() {
expect(1); expect(1);
var div = $("#foo")[0]; var div = $("#foo")[0];
@ -1405,6 +1435,27 @@ test("$.removeData", function() {
ok( jQuery.data(div, "test") == undefined, "Check removal of data" ); ok( jQuery.data(div, "test") == undefined, "Check removal of data" );
}); });
test(".removeData()", function() {
expect(6);
var div = $("#foo");
div.data("test", "testing");
div.removeData("test");
ok( div.data("test") == undefined, "Check removal of data" );
div.data("test", "testing");
div.data("test.foo", "testing2");
div.removeData("test.bar");
ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
ok( div.data("test") == "testing", "Make sure data is intact" );
div.removeData("test");
ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
ok( div.data("test") == undefined, "Make sure data is intact" );
div.removeData("test.foo");
ok( div.data("test.foo") == undefined, "Make sure data is intact" );
});
test("remove()", function() { test("remove()", function() {
expect(6); expect(6);
$("#ap").children().remove(); $("#ap").children().remove();