Bug Tracker

Opened 15 years ago

Closed 14 years ago

#2286 closed enhancement (fixed)

Add ability to use "Category" namespacing on events

Reported by: cthatcher Owned by:
Priority: minor Milestone: 1.2.3
Component: event Version: 1.2.2
Keywords: event namespace Cc:
Blocked by: Blocking:


the milestone / version are actually 1.2.4 / 1.2.3 respectively (the trac hasnt been updated yet as 1.2.3 ment out minutes ago).

This excerpt is taken from the dev list thread "[jquery-dev] Re: $().unbind('.namespace') added", which was initally an announcement from Scott Gonzalez of the new feature to allow developers unbind events registered to namespaces.

I had mentioned this a little while ago and it might have slipped by or it might have been graciously left alone because it's not a good idea, but internally I usually have two namespaces so each module has something like Outer.Inner.Class
and I generally use the namespace, class and event to bind, so:

jQuery.bind("myevent.Outer", function(){...});
jQuery.bind("myevent.Outer.Inner", function(){...});
jQuery.bind("myevent.Outer.Inner.Class", function(){...});

Right now jQuery.event has

64	                // Namespaced event handlers
65	                var parts = type.split(".");
66	                type = parts[0];
67	                handler.type = parts[1];

which means, in my case only Outer is ever included in the namespace registering. Then it's unbind all, or unbind nothing for the namespaced events. If instead it were:

64	                // Namespaced event handlers
65	                var parts = type.split(".");
66	                type = parts.shift();
67	                handler.type = parts.join(".");

I'd be able to do:


which of course would require the remove was changed to:

                   // Namespaced event handlers
126	                    var parts = type.split(".");
127	                    type = parts.shift();
			    parts = parts.join(".");
129	                    if ( events[type] ) {
130	                        // remove the given handler for the given type
131	                        if ( handler )
132	                            delete events[type][handler.guid];
134	                        // remove all handlers for the given type
135	                        else
136	                            for ( handler in events[type] )
137	                                // Handle the removal of namespaced events
138	                                if ( !parts || events[type][handler].type == parts )
139	                                    delete events[type][handler];

I'd love to hear your thoughts, especially if it's a bad idea so I can understand why. 

Chris Thatcher
[email protected]

Change History (1)

comment:1 Changed 14 years ago by dmethvin

Resolution: fixed
Status: newclosed

As I understand the desired feature, this was implemented as part of jQuery 1.3.

Note: See TracTickets for help on using tickets.