Skip to main content

Bug Tracker

Side navigation

#5834 closed bug (fixed)

Opened January 16, 2010 06:36AM UTC

Closed January 25, 2010 10:02PM UTC

event.special namespacing bug

Reported by: cowboy Owned by:
Priority: major Milestone: 1.4.1
Component: event Version: 1.4
Keywords: Cc:
Blocked by: Blocking:
Description

Perhaps I'm missing something, but an jQuery.event.special add method returns a function, namespacing no longer works.

This image represents what "should" work, but notice that namespacing seems to have no effect when triggering or unbinding:

http://benalman.com/grab/150c27.png

If add doesn't return a function, namespacing seems to work.. but of course the event object is no longer being augmented:

http://benalman.com/grab/ac7431.png

Here is the code I used to test all this:

console.log( $.fn.jquery );
var i = 0;

jQuery.event.special.myevent = {
  setup: function(){
    console.log( 'setup', this.tagName, [].slice.call( arguments ) );
  }, 
  teardown: function(){
    console.log( 'teardown', this.tagName, [].slice.call( arguments ) );
  },
  add: function( handler, data, namespaces ){
    console.log( 'add', this.tagName, [].slice.call( arguments ) );
    return; // NAMESPACING ONLY WORKS IF I RETURN HERE
    return function(e){
      e.data = data;
      e.xyz = ++i;
      handler.apply( this, arguments );
    }
  },
  remove: function(){
    console.log( 'remove', this.tagName, [].slice.call( arguments ) );
  }
};

function a(e){ console.log( 'a', e.xyz, e.data ); }
function b(e){ console.log( 'b', e.xyz, e.data ); }
function bc(e){ console.log( 'bc', e.xyz, e.data ); }

var elem = $('body');

elem.bind( 'myevent.a', {x:1}, a);
elem.trigger( 'myevent' );
elem.trigger( 'myevent.a' );
elem.bind( 'myevent.b', {y:2}, b);
elem.trigger( 'myevent' );
elem.trigger( 'myevent.b' );
elem.bind( 'myevent.b.c', {z:3}, bc);
elem.trigger( 'myevent' );
elem.trigger( 'myevent.b' );
elem.trigger( 'myevent.b.c' );
elem.unbind( 'myevent.a' );
elem.trigger( 'myevent' );
elem.unbind( 'myevent', b );
elem.trigger( 'myevent' );
elem.unbind( 'myevent.b.c' );
elem.trigger( 'myevent' );

On a separate but related note, when the add "function return" is used to augment the event object, is there any way to automatically propagate event.data? It feels like an unnecessary "extra" step to me.

Attachments (0)
Change History (2)

Changed January 16, 2010 06:40AM UTC by cowboy comment:1

And in case this is reviewed by someone who isn't super familiar with jQuery.event.special, I've been working on an article explaining a bit about how Special Events work. This screenshot may help explain what the expected behavior of the setup, teardown, add and remove methods should be:

http://benalman.com/grab/7d7089.png

(*please* correct me if I've made any mistakes!)

Changed January 25, 2010 10:02PM UTC by john comment:2

component: unfilledevent
resolution: → fixed
status: newclosed

Both points fixed - you no longer have to copy over data anymore.

http://github.com/jquery/jquery/commit/390186b902c4c1ac13e23754d33ed4d8b3d5fa38