Bug Tracker

Ticket #6006: 0001-added-object-as-event-fn-pairs-support-to-delegate.patch

File 0001-added-object-as-event-fn-pairs-support-to-delegate.patch, 5.0 KB (added by deadlyicon, 12 years ago)

patch on top of f9417b9da980f4fa79d03ba691d9f3442c5ab2f2

  • src/event.js

    From d3825ef41b82a220e6d116c33514c8de03d81da2 Mon Sep 17 00:00:00 2001
    From: Jared Grippe <jared@deadlyicon.com>
    Date: Mon, 1 Feb 2010 16:05:56 -0800
    Subject: [PATCH] added 'object as event/fn pairs' support to delegate
    
    ---
     src/event.js       |    7 ++++++-
     test/unit/event.js |   24 ++++++++++++++++++++++--
     2 files changed, 28 insertions(+), 3 deletions(-)
    
    diff --git a/src/event.js b/src/event.js
    index f6b3d31..c790c0f 100644
    a b jQuery.fn.extend({ 
    868868        },
    869869       
    870870        delegate: function( selector, types, data, fn ) {
    871                 return this.live( types, data, fn, selector );
     871                if (jQuery.isPlainObject(types)){
     872                        for (var type in types) this.live( type, undefined, types[type], selector );
     873                }else{
     874                        this.live( types, data, fn, selector );
     875                }
     876                return this;
    872877        },
    873878       
    874879        undelegate: function( selector, types, fn ) {
  • test/unit/event.js

    diff --git a/test/unit/event.js b/test/unit/event.js
    index fbb156f..4ad1ff1 100644
    a b test("live with submit", function() { 
    11261126});
    11271127
    11281128test(".delegate()/.undelegate()", function() {
    1129         expect(65);
     1129        expect(81);
    11301130
    1131         var submit = 0, div = 0, livea = 0, liveb = 0;
     1131        var submit = div = livea = liveb = livec = lived = 0;
    11321132
    11331133        jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
    11341134        jQuery("#body").delegate("div", "click", function(){ div++; });
    11351135        jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
    11361136        jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
     1137        jQuery("#body").delegate("div#nothiddendivchild", {
     1138          click: function(){ livec++; },
     1139          'click hover': function(){ lived++; }
     1140        });
    11371141
    11381142        // Nothing should trigger on the body
    11391143        jQuery("body").trigger("click");
    test(".delegate()/.undelegate()", function() { 
    11411145        equals( div, 0, "Click on body" );
    11421146        equals( livea, 0, "Click on body" );
    11431147        equals( liveb, 0, "Click on body" );
     1148        equals( livec, 0, "Click on body" );
     1149        equals( lived, 0, "Click on body" );
    11441150
    11451151        // This should trigger two events
    11461152        jQuery("div#nothiddendiv").trigger("click");
    test(".delegate()/.undelegate()", function() { 
    11481154        equals( div, 1, "Click on div" );
    11491155        equals( livea, 1, "Click on div" );
    11501156        equals( liveb, 0, "Click on div" );
     1157        equals( livec, 0, "Click on body" );
     1158        equals( lived, 0, "Click on body" );
    11511159
    11521160        // This should trigger three events (w/ bubbling)
    11531161        jQuery("div#nothiddendivchild").trigger("click");
    test(".delegate()/.undelegate()", function() { 
    11551163        equals( div, 2, "Click on inner div" );
    11561164        equals( livea, 2, "Click on inner div" );
    11571165        equals( liveb, 1, "Click on inner div" );
     1166        equals( livec, 1, "Click on body" );
     1167        equals( lived, 1, "Click on body" );
    11581168
    11591169        // This should trigger one submit
    11601170        jQuery("div#nothiddendivchild").trigger("submit");
    test(".delegate()/.undelegate()", function() { 
    11621172        equals( div, 2, "Submit on div" );
    11631173        equals( livea, 2, "Submit on div" );
    11641174        equals( liveb, 1, "Submit on div" );
     1175        equals( livec, 1, "Click on body" );
     1176        equals( lived, 1, "Click on body" );
    11651177
    11661178        // Make sure no other events were removed in the process
    11671179        jQuery("div#nothiddendivchild").trigger("click");
    test(".delegate()/.undelegate()", function() { 
    11691181        equals( div, 3, "undelegate Click on inner div" );
    11701182        equals( livea, 3, "undelegate Click on inner div" );
    11711183        equals( liveb, 2, "undelegate Click on inner div" );
     1184        equals( livec, 2, "undelegate Click on inner div" );
     1185        equals( lived, 2, "undelegate Click on inner div" );
    11721186
    11731187        // Now make sure that the removal works
    11741188        jQuery("#body").undelegate("div#nothiddendivchild", "click");
    test(".delegate()/.undelegate()", function() { 
    11771191        equals( div, 4, "undelegate Click on inner div" );
    11781192        equals( livea, 4, "undelegate Click on inner div" );
    11791193        equals( liveb, 2, "undelegate Click on inner div" );
     1194        equals( livec, 2, "undelegate Click on inner div" );
     1195        equals( lived, 2, "undelegate Click on inner div" );
    11801196
    11811197        // Make sure that the click wasn't removed too early
    11821198        jQuery("div#nothiddendiv").trigger("click");
    test(".delegate()/.undelegate()", function() { 
    11841200        equals( div, 5, "undelegate Click on inner div" );
    11851201        equals( livea, 5, "undelegate Click on inner div" );
    11861202        equals( liveb, 2, "undelegate Click on inner div" );
     1203        equals( livec, 2, "undelegate Click on inner div" );
     1204        equals( lived, 2, "undelegate Click on inner div" );
    11871205
    11881206        // Make sure that stopPropgation doesn't stop live events
    11891207        jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
    test(".delegate()/.undelegate()", function() { 
    11921210        equals( div, 6, "stopPropagation Click on inner div" );
    11931211        equals( livea, 6, "stopPropagation Click on inner div" );
    11941212        equals( liveb, 3, "stopPropagation Click on inner div" );
     1213        equals( livec, 2, "stopPropagation Click on inner div" );
     1214        equals( lived, 2, "stopPropagation Click on inner div" );
    11951215
    11961216        // Make sure click events only fire with primary click
    11971217        var event = jQuery.Event("click");