You can now overwrite values returned from .data() with .bind("getData") - returning a value will override any bound value on that element.
This commit is contained in:
parent
4a11e6d70b
commit
b0c7df65d0
2 changed files with 30 additions and 11 deletions
17
src/core.js
17
src/core.js
|
@ -480,16 +480,19 @@ jQuery.fn = jQuery.prototype = {
|
||||||
|
|
||||||
data: function( key, value ){
|
data: function( key, value ){
|
||||||
var parts = key.split(".");
|
var parts = key.split(".");
|
||||||
|
parts[1] = parts[1] ? "." + parts[1] : "";
|
||||||
|
|
||||||
if ( value == null ) {
|
if ( value == null ) {
|
||||||
if ( this.length ) {
|
var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
|
||||||
var data = jQuery.data( this[0], key );
|
|
||||||
return data == null ?
|
if ( data == undefined && this.length )
|
||||||
jQuery.data( this[0], parts[0] ) :
|
data = jQuery.data( this[0], key );
|
||||||
data;
|
|
||||||
}
|
return data == null && parts[1] ?
|
||||||
|
this.data( parts[0] ) :
|
||||||
|
data;
|
||||||
} else
|
} else
|
||||||
return this.trigger("setData" + (parts[1] ? "." + parts[1] : "") + "!", [parts[0], value]).each(function(){
|
return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
|
||||||
jQuery.data( this, key, value );
|
jQuery.data( this, key, value );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -1398,7 +1398,7 @@ test("$.data", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test(".data()", function() {
|
test(".data()", function() {
|
||||||
expect(11);
|
expect(16);
|
||||||
var div = $("#foo");
|
var div = $("#foo");
|
||||||
ok( div.data("test") == undefined, "Check for no data exists" );
|
ok( div.data("test") == undefined, "Check for no data exists" );
|
||||||
div.data("test", "success");
|
div.data("test", "success");
|
||||||
|
@ -1406,25 +1406,41 @@ test(".data()", function() {
|
||||||
div.data("test", "overwritten");
|
div.data("test", "overwritten");
|
||||||
ok( div.data("test") == "overwritten", "Check for overwritten data" );
|
ok( div.data("test") == "overwritten", "Check for overwritten data" );
|
||||||
|
|
||||||
var hits = {test:0};
|
var hits = {test:0}, gets = {test:0};
|
||||||
|
|
||||||
div
|
div
|
||||||
.bind("setData",function(e,key,value){ hits[key] += value; })
|
.bind("setData",function(e,key,value){ hits[key] += value; })
|
||||||
.bind("setData.foo",function(e,key,value){ hits[key] += value; })
|
.bind("setData.foo",function(e,key,value){ hits[key] += value; })
|
||||||
|
.bind("getData",function(e,key){ gets[key] += 1; })
|
||||||
|
.bind("getData.foo",function(e,key){ gets[key] += 3; });
|
||||||
|
|
||||||
div.data("test.foo", 2);
|
div.data("test.foo", 2);
|
||||||
ok( div.data("test") == "overwritten", "Check for original data" );
|
ok( div.data("test") == "overwritten", "Check for original data" );
|
||||||
ok( div.data("test.foo") == 2, "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( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
|
||||||
ok( hits.test == 2, "Check triggered functions" );
|
equals( hits.test, 2, "Check triggered setter functions" );
|
||||||
|
equals( gets.test, 5, "Check triggered getter functions" );
|
||||||
|
|
||||||
hits.test = 0;
|
hits.test = 0;
|
||||||
|
gets.test = 0;
|
||||||
|
|
||||||
div.data("test", 1);
|
div.data("test", 1);
|
||||||
ok( div.data("test") == 1, "Check for original data" );
|
ok( div.data("test") == 1, "Check for original data" );
|
||||||
ok( div.data("test.foo") == 2, "Check for namespaced data" );
|
ok( div.data("test.foo") == 2, "Check for namespaced data" );
|
||||||
ok( div.data("test.bar") == 1, "Check for unmatched namespace" );
|
ok( div.data("test.bar") == 1, "Check for unmatched namespace" );
|
||||||
ok( hits.test == 1, "Check triggered functions" );
|
equals( hits.test, 1, "Check triggered setter functions" );
|
||||||
|
equals( gets.test, 5, "Check triggered getter functions" );
|
||||||
|
|
||||||
|
hits.test = 0;
|
||||||
|
gets.test = 0;
|
||||||
|
|
||||||
|
div
|
||||||
|
.bind("getData",function(e,key){ return key + "root"; })
|
||||||
|
.bind("getData.foo",function(e,key){ return key + "foo"; });
|
||||||
|
|
||||||
|
ok( div.data("test") == "testroot", "Check for original data" );
|
||||||
|
ok( div.data("test.foo") == "testfoo", "Check for namespaced data" );
|
||||||
|
ok( div.data("test.bar") == "testroot", "Check for unmatched namespace" );
|
||||||
});
|
});
|
||||||
|
|
||||||
test("$.removeData", function() {
|
test("$.removeData", function() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue