Bug Tracker

Changeset 4600

Show
Ignore:
Timestamp:
02/03/08 04:05:24 (10 months ago)
Author:
jeresig
Message:

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).

Location:
trunk/jquery
Files:
3 modified

Legend:

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

    r4586 r4600  
    477477    andSelf: function() { 
    478478        return this.add( this.prevObject ); 
     479    }, 
     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        }); 
    479499    }, 
    480500     
  • trunk/jquery/src/event.js

    r4440 r4600  
    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 ) { 
     
    192197            // Enforce the right trigger type 
    193198            data[0].type = type; 
     199            if ( exclusive ) 
     200                data[0].exclusive = true; 
    194201 
    195202            // Trigger the event 
     
    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 
  • trunk/jquery/test/unit/core.js

    r4513 r4600  
    13981398}); 
    13991399 
     1400test(".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 
    14001430test("$.removeData", function() { 
    14011431    expect(1); 
     
    14041434    jQuery.removeData(div, "test"); 
    14051435    ok( jQuery.data(div, "test") == undefined, "Check removal of data" ); 
     1436}); 
     1437 
     1438test(".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" ); 
    14061457}); 
    14071458