Bug Tracker

Changeset 4001

Show
Ignore:
Timestamp:
12/03/07 21:41:10 (9 months ago)
Author:
davidserduke
Message:

Fixed [1993] although it actually wasn't a bug in the core but rather a misunderstanding of how the extra function was supposed to work in jQuery.event.trigger(). That said, it seems more useful and robust for the code to work the way the ticket author thought it should work so this change was made.

Now, if anything is returned from the extra function it will overwrite the return value of the event handlers. This should only effect custom events unless someone had an extra function that returned a value other than false which would have been ignored before.

Location:
trunk/jquery
Files:
2 modified

Legend:

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

    r3970 r4001  
    172172 
    173173            // Handle triggering of extra function 
    174             if ( extra && extra.apply( element, data ) === false ) 
    175                 val = false; 
     174            if ( extra ) { 
     175                // call the extra function and tack the current return value on the end for possible inspection 
     176                var ret = extra.apply( element, data.concat( val ) ); 
     177                // if anything is returned, give it precedence and have it overwrite the previous value 
     178                if (ret !== undefined) 
     179                    val = ret; 
     180            } 
    176181 
    177182            // Trigger the native events (except for clicks on links) 
  • trunk/jquery/test/unit/event.js

    r3970 r4001  
    3232    //  
    3333    // doc.body.innerHTML = "<input type='text'/>"; 
    34     //   
     34    // 
    3535    // var input = doc.getElementsByTagName("input")[0]; 
    36     //   
     36    // 
    3737    // $(input).bind("click",function() { 
    3838    //  ok( true, "Binding to element inside iframe" ); 
     
    4949    reset(); 
    5050 
    51         $("#firstp").bind("click",function(e){ 
     51    $("#firstp").bind("click",function(e){ 
    5252        ok(true, "Normal click triggered"); 
    53         }); 
    54  
    55         $("#firstp").bind("click.test",function(e){ 
     53    }); 
     54 
     55    $("#firstp").bind("click.test",function(e){ 
    5656        ok(true, "Namespaced click triggered"); 
    57         }); 
     57    }); 
    5858 
    5959    // Trigger both bound fn (2) 
    60         $("#firstp").trigger("click"); 
     60    $("#firstp").trigger("click"); 
    6161 
    6262    // Trigger one bound fn (1) 
    63         $("#firstp").trigger("click.test"); 
     63    $("#firstp").trigger("click.test"); 
    6464 
    6565    // Remove only the one fn 
    66         $("#firstp").unbind("click.test"); 
     66    $("#firstp").unbind("click.test"); 
    6767 
    6868    // Trigger the remaining fn (1) 
    69         $("#firstp").trigger("click"); 
     69    $("#firstp").trigger("click"); 
    7070}); 
    7171 
     
    7373    expect(4); 
    7474    $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() { 
    75         var close = $('spanx', this); // same with $(this).find('span'); 
    76         ok( close.length == 0, "Context element does not exist, length must be zero" ); 
    77         ok( !close[0], "Context element does not exist, direct access to element must return undefined" ); 
    78         return false; 
     75        var close = $('spanx', this); // same with $(this).find('span'); 
     76        ok( close.length == 0, "Context element does not exist, length must be zero" ); 
     77        ok( !close[0], "Context element does not exist, direct access to element must return undefined" ); 
     78        return false; 
    7979    }).click(); 
    8080     
     
    117117 
    118118test("trigger(event, [data], [fn])", function() { 
    119     expect(40); 
     119    expect(66); 
    120120 
    121121    var handler = function(event, a, b, c) { 
     
    131131        equals( b, "2", "check passed data" ); 
    132132        equals( c, "abc", "check passed data" ); 
    133         return "test2"; 
     133        return false; 
     134    }; 
     135 
     136    var handler3 = function(a, b, c, v) { 
     137        equals( a, 1, "check passed data" ); 
     138        equals( b, "2", "check passed data" ); 
     139        equals( c, "abc", "check passed data" ); 
     140        equals( v, "test", "check current value" ); 
     141        return "newVal"; 
     142    }; 
     143 
     144    var handler4 = function(a, b, c, v) { 
     145        equals( a, 1, "check passed data" ); 
     146        equals( b, "2", "check passed data" ); 
     147        equals( c, "abc", "check passed data" ); 
     148        equals( v, "test", "check current value" ); 
    134149    }; 
    135150 
     
    144159 
    145160    // Triggers handlers, native, and extra fn 
    146     // Triggers 8 
    147     $("#firstp").trigger("click", [1, "2", "abc"], handler2); 
     161    // Triggers 9 
     162    $("#firstp").trigger("click", [1, "2", "abc"], handler4); 
    148163 
    149164    // Simulate a "native" click 
     
    152167    }; 
    153168 
     169    // Triggers handlers, native, and extra fn 
     170    // Triggers 7 
     171    $("#firstp").trigger("click", [1, "2", "abc"], handler2); 
     172 
    154173    // Trigger only the handlers (no native) 
    155174    // Triggers 5 
     
    158177    // Trigger only the handlers (no native) and extra fn 
    159178    // Triggers 8 
    160     equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), "test", "Verify handler response" ); 
     179    equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" ); 
    161180 
    162181    // Build fake click event to pass in 
     
    170189    // Triggers 9 
    171190    equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" ); 
     191 
     192    // have the extra handler override the return 
     193    // Triggers 9 
     194    equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" ); 
     195 
     196    // have the extra handler leave the return value alone 
     197    // Triggers 9 
     198    equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" ); 
    172199}); 
    173200