Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

You can now overwrite values returned from .data() with .bind("getDat…

…a") - returning a value will override any bound value on that element.
  • Loading branch information...
commit b0c7df65d0f92b77752e8f9b33898d0e3b2c05e8 1 parent 4a11e6d
John Resig jeresig authored

Showing 2 changed files with 30 additions and 11 deletions. Show diff stats Hide diff stats

  1. +10 7 src/core.js
  2. +20 4 test/unit/core.js
17 src/core.js
@@ -480,16 +480,19 @@ jQuery.fn = jQuery.prototype = {
480 480
481 481 data: function( key, value ){
482 482 var parts = key.split(".");
  483 + parts[1] = parts[1] ? "." + parts[1] : "";
483 484
484 485 if ( value == null ) {
485   - if ( this.length ) {
486   - var data = jQuery.data( this[0], key );
487   - return data == null ?
488   - jQuery.data( this[0], parts[0] ) :
489   - data;
490   - }
  486 + var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
  487 +
  488 + if ( data == undefined && this.length )
  489 + data = jQuery.data( this[0], key );
  490 +
  491 + return data == null && parts[1] ?
  492 + this.data( parts[0] ) :
  493 + data;
491 494 } else
492   - return this.trigger("setData" + (parts[1] ? "." + parts[1] : "") + "!", [parts[0], value]).each(function(){
  495 + return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
493 496 jQuery.data( this, key, value );
494 497 });
495 498 },
24 test/unit/core.js
@@ -1398,7 +1398,7 @@ test("$.data", function() {
1398 1398 });
1399 1399
1400 1400 test(".data()", function() {
1401   - expect(11);
  1401 + expect(16);
1402 1402 var div = $("#foo");
1403 1403 ok( div.data("test") == undefined, "Check for no data exists" );
1404 1404 div.data("test", "success");
@@ -1406,25 +1406,41 @@ test(".data()", function() {
1406 1406 div.data("test", "overwritten");
1407 1407 ok( div.data("test") == "overwritten", "Check for overwritten data" );
1408 1408
1409   - var hits = {test:0};
  1409 + var hits = {test:0}, gets = {test:0};
1410 1410
1411 1411 div
1412 1412 .bind("setData",function(e,key,value){ hits[key] += value; })
1413 1413 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
  1414 + .bind("getData",function(e,key){ gets[key] += 1; })
  1415 + .bind("getData.foo",function(e,key){ gets[key] += 3; });
1414 1416
1415 1417 div.data("test.foo", 2);
1416 1418 ok( div.data("test") == "overwritten", "Check for original data" );
1417 1419 ok( div.data("test.foo") == 2, "Check for namespaced data" );
1418 1420 ok( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
1419   - ok( hits.test == 2, "Check triggered functions" );
  1421 + equals( hits.test, 2, "Check triggered setter functions" );
  1422 + equals( gets.test, 5, "Check triggered getter functions" );
1420 1423
1421 1424 hits.test = 0;
  1425 + gets.test = 0;
1422 1426
1423 1427 div.data("test", 1);
1424 1428 ok( div.data("test") == 1, "Check for original data" );
1425 1429 ok( div.data("test.foo") == 2, "Check for namespaced data" );
1426 1430 ok( div.data("test.bar") == 1, "Check for unmatched namespace" );
1427   - ok( hits.test == 1, "Check triggered functions" );
  1431 + equals( hits.test, 1, "Check triggered setter functions" );
  1432 + equals( gets.test, 5, "Check triggered getter functions" );
  1433 +
  1434 + hits.test = 0;
  1435 + gets.test = 0;
  1436 +
  1437 + div
  1438 + .bind("getData",function(e,key){ return key + "root"; })
  1439 + .bind("getData.foo",function(e,key){ return key + "foo"; });
  1440 +
  1441 + ok( div.data("test") == "testroot", "Check for original data" );
  1442 + ok( div.data("test.foo") == "testfoo", "Check for namespaced data" );
  1443 + ok( div.data("test.bar") == "testroot", "Check for unmatched namespace" );
1428 1444 });
1429 1445
1430 1446 test("$.removeData", function() {

0 comments on commit b0c7df6

Please sign in to comment.
Something went wrong with that request. Please try again.