Skip to content

Commit 77da945

Browse files
committed
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).
1 parent 25c7623 commit 77da945

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/core.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,26 @@ jQuery.fn = jQuery.prototype = {
477477
andSelf: function() {
478478
return this.add( this.prevObject );
479479
},
480+
481+
data: function( key, value ){
482+
if ( value == null ) {
483+
if ( this.length ) {
484+
var data = jQuery.data( this[0], key );
485+
return data == null ?
486+
jQuery.data( this[0], key.split(".")[0] ) :
487+
data;
488+
}
489+
} else
490+
return this.trigger("set-" + key + "!", [value]).each(function(){
491+
jQuery.data( this, key, value );
492+
});
493+
},
494+
495+
removeData: function( key ){
496+
return this.each(function(){
497+
jQuery.removeData( this, key );
498+
});
499+
},
480500

481501
domManip: function( args, table, reverse, callback ) {
482502
var clone = this.length > 1, elems;

src/event.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ jQuery.event = {
169169
// Clone the incoming data, if any
170170
data = jQuery.makeArray(data || []);
171171

172+
if ( type.indexOf("!") >= 0 ) {
173+
type = type.slice(0, -1);
174+
var exclusive = true;
175+
}
176+
172177
// Handle a global trigger
173178
if ( !elem ) {
174179
// Only trigger if we've ever bound an event for it
@@ -191,6 +196,8 @@ jQuery.event = {
191196

192197
// Enforce the right trigger type
193198
data[0].type = type;
199+
if ( exclusive )
200+
data[0].exclusive = true;
194201

195202
// Trigger the event
196203
if ( jQuery.isFunction( jQuery.data(elem, "handle") ) )
@@ -250,7 +257,7 @@ jQuery.event = {
250257
args[0].data = handler.data;
251258

252259
// Filter the functions by class
253-
if ( !parts[1] || handler.type == parts[1] ) {
260+
if ( !parts[1] && !event.exclusive || handler.type == parts[1] ) {
254261
var ret = handler.apply( this, args );
255262

256263
if ( val !== false )

test/unit/core.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,36 @@ test("$.data", function() {
13971397
ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" );
13981398
});
13991399

1400+
test(".data()", function() {
1401+
expect(11);
1402+
var div = $("#foo");
1403+
ok( div.data("test") == undefined, "Check for no data exists" );
1404+
div.data("test", "success");
1405+
ok( div.data("test") == "success", "Check for added data" );
1406+
div.data("test", "overwritten");
1407+
ok( div.data("test") == "overwritten", "Check for overwritten data" );
1408+
1409+
var hits = 0;
1410+
1411+
div
1412+
.bind("set-test",function(){ hits += 1; })
1413+
.bind("set-test.foo",function(){ hits += 2; })
1414+
1415+
div.data("test.foo", "foodata");
1416+
ok( div.data("test") == "overwritten", "Check for original data" );
1417+
ok( div.data("test.foo") == "foodata", "Check for namespaced data" );
1418+
ok( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
1419+
ok( hits == 2, "Check triggered functions" );
1420+
1421+
hits = 0;
1422+
1423+
div.data("test", "overwritten2");
1424+
ok( div.data("test") == "overwritten2", "Check for original data" );
1425+
ok( div.data("test.foo") == "foodata", "Check for namespaced data" );
1426+
ok( div.data("test.bar") == "overwritten2", "Check for unmatched namespace" );
1427+
ok( hits == 1, "Check triggered functions" );
1428+
});
1429+
14001430
test("$.removeData", function() {
14011431
expect(1);
14021432
var div = $("#foo")[0];
@@ -1405,6 +1435,27 @@ test("$.removeData", function() {
14051435
ok( jQuery.data(div, "test") == undefined, "Check removal of data" );
14061436
});
14071437

1438+
test(".removeData()", function() {
1439+
expect(6);
1440+
var div = $("#foo");
1441+
div.data("test", "testing");
1442+
div.removeData("test");
1443+
ok( div.data("test") == undefined, "Check removal of data" );
1444+
1445+
div.data("test", "testing");
1446+
div.data("test.foo", "testing2");
1447+
div.removeData("test.bar");
1448+
ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
1449+
ok( div.data("test") == "testing", "Make sure data is intact" );
1450+
1451+
div.removeData("test");
1452+
ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
1453+
ok( div.data("test") == undefined, "Make sure data is intact" );
1454+
1455+
div.removeData("test.foo");
1456+
ok( div.data("test.foo") == undefined, "Make sure data is intact" );
1457+
});
1458+
14081459
test("remove()", function() {
14091460
expect(6);
14101461
$("#ap").children().remove();

0 commit comments

Comments
 (0)