aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2008-02-03 17:56:21 +0000
committerJohn Resig <jeresig@gmail.com>2008-02-03 17:56:21 +0000
commit4a11e6d70b5c4b1473b62050929cffe3d2a67317 (patch)
tree157bdcf8fee190cd1d7ba3904d168cdc7b505b40
parent703e89ba302e86bba1f4777ce1a6aee6f38b7b87 (diff)
downloadjquery-4a11e6d70b5c4b1473b62050929cffe3d2a67317.tar.gz
jquery-4a11e6d70b5c4b1473b62050929cffe3d2a67317.zip
Tweaked the .data() event triggering - it now triggers a single 'setData' event, passing in a key value pair of what was changed.
-rw-r--r--src/core.js6
-rw-r--r--test/unit/core.js24
2 files changed, 16 insertions, 14 deletions
diff --git a/src/core.js b/src/core.js
index f36edefa3..6997f8907 100644
--- a/src/core.js
+++ b/src/core.js
@@ -479,15 +479,17 @@ jQuery.fn = jQuery.prototype = {
},
data: function( key, value ){
+ var parts = key.split(".");
+
if ( value == null ) {
if ( this.length ) {
var data = jQuery.data( this[0], key );
return data == null ?
- jQuery.data( this[0], key.split(".")[0] ) :
+ jQuery.data( this[0], parts[0] ) :
data;
}
} else
- return this.trigger("set-" + key + "!", [value]).each(function(){
+ return this.trigger("setData" + (parts[1] ? "." + parts[1] : "") + "!", [parts[0], value]).each(function(){
jQuery.data( this, key, value );
});
},
diff --git a/test/unit/core.js b/test/unit/core.js
index 80e92e6be..3ca1e9510 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -1406,25 +1406,25 @@ test(".data()", function() {
div.data("test", "overwritten");
ok( div.data("test") == "overwritten", "Check for overwritten data" );
- var hits = 0;
+ var hits = {test:0};
div
- .bind("set-test",function(){ hits += 1; })
- .bind("set-test.foo",function(){ hits += 2; })
+ .bind("setData",function(e,key,value){ hits[key] += value; })
+ .bind("setData.foo",function(e,key,value){ hits[key] += value; })
- div.data("test.foo", "foodata");
+ div.data("test.foo", 2);
ok( div.data("test") == "overwritten", "Check for original data" );
- ok( div.data("test.foo") == "foodata", "Check for namespaced data" );
+ ok( div.data("test.foo") == 2, "Check for namespaced data" );
ok( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
- ok( hits == 2, "Check triggered functions" );
+ ok( hits.test == 2, "Check triggered functions" );
- hits = 0;
+ hits.test = 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" );
+ div.data("test", 1);
+ ok( div.data("test") == 1, "Check for original data" );
+ ok( div.data("test.foo") == 2, "Check for namespaced data" );
+ ok( div.data("test.bar") == 1, "Check for unmatched namespace" );
+ ok( hits.test == 1, "Check triggered functions" );
});
test("$.removeData", function() {