Bug Tracker

Changeset 4611 for trunk/jquery

Show
Ignore:
Timestamp:
02/03/08 18:43:04 (1 year ago)
Author:
jeresig
Message:

You can now overwrite values returned from .data() with .bind("getData") - returning a value will override any bound value on that element.

Location:
trunk/jquery
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/core.js

    r4606 r4611  
    481481    data: function( key, value ){ 
    482482        var parts = key.split("."); 
     483        parts[1] = parts[1] ? "." + parts[1] : ""; 
    483484 
    484485        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; 
    491494        } 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(){ 
    493496                jQuery.data( this, key, value ); 
    494497            }); 
  • trunk/jquery/test/unit/core.js

    r4606 r4611  
    13991399 
    14001400test(".data()", function() { 
    1401     expect(11); 
     1401    expect(16); 
    14021402    var div = $("#foo"); 
    14031403    ok( div.data("test") == undefined, "Check for no data exists" ); 
     
    14071407    ok( div.data("test") == "overwritten", "Check for overwritten data" ); 
    14081408 
    1409     var hits = {test:0}; 
     1409    var hits = {test:0}, gets = {test:0}; 
    14101410 
    14111411    div 
    14121412        .bind("setData",function(e,key,value){ hits[key] += value; }) 
    14131413        .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; }); 
    14141416 
    14151417    div.data("test.foo", 2); 
     
    14171419    ok( div.data("test.foo") == 2, "Check for namespaced data" ); 
    14181420    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" ); 
    14201423 
    14211424    hits.test = 0; 
     1425    gets.test = 0; 
    14221426 
    14231427    div.data("test", 1); 
     
    14251429    ok( div.data("test.foo") == 2, "Check for namespaced data" ); 
    14261430    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" ); 
    14281444}); 
    14291445