Bug Tracker

Opened 13 years ago

Closed 13 years ago

#5834 closed bug (fixed)

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:


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

Change History (2)

comment:1 Changed 13 years ago by cowboy

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:


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

comment:2 Changed 13 years ago by john

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

Note: See TracTickets for help on using tickets.