Bug Tracker

Opened 12 years ago

Closed 12 years ago

#2585 closed enhancement (invalid)

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.

Change History (2)

comment:1 Changed 12 years ago by krudd

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');

comment:2 Changed 12 years ago by scott.gonzal

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.

Note: See TracTickets for help on using tickets.