Skip to main content

Bug Tracker

Side navigation

#2585 closed enhancement (invalid)

Opened March 25, 2008 08:57PM UTC

Closed March 26, 2008 03:08PM UTC

Expose the namespaced event type on event object - easy and useful!

Reported by: guy.fraser Owned by:
Priority: major Milestone: 1.2.4
Component: event Version: 1.2.3
Keywords: event namespace enhancement Cc:
Blocked by: Blocking:
Description

If I'm using one handler to listen to several events, it would be super useful if I could switch based on the namespaced event type. For example:

var foo = $('.foo');

foo.bind('custom',function(event) {

switch (event.namespace) {

case 'custom.show':

stuff to do on show

beak;

case 'custom.hide':

stuff to do on show

beak;

}

});

foo.trigger('custom.show');

foo.trigger('custom.hide');

I know I can pass in data when triggering the event, but this seems a bloaty way to do things when the events system already knows internally what the namespaced event type is.

Exposing this on the event object is extremely trivial. In the handler() method in the current public release of jQuery 1.2.3 just store the original type, before splitting in to it's parts, before line 2059:

event.namespace = event.type;

Voila! Super simple and does away with the need for messy data objects. This would make it trivial to listen to all events from a UI control, for example, with a single method that just switches based on the event namespace.

Attachments (0)
Change History (2)

Changed March 25, 2008 11:45PM UTC by krudd comment:1

This is could be a useful addition to the custom events.

However, as I noted in the Google Groups post, you have the namespace and event type around the wrong way. It should be:

foo.bind('show.custom',function(event) { /* ... */ } );
foo.trigger('show.custom');

Changed March 26, 2008 03:08PM UTC by scott.gonzal comment:2

resolution: → invalid
status: newclosed

This would break the intended functionality of triggering a namespaced event. If you specify a namespace when triggering an event, only handlers bound with that namespace should be triggered.