Bug Tracker

Modify

Ticket #2585 (closed enhancement: invalid)

Opened 5 years ago

Last modified 5 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 5 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 5 years ago by scott.gonzal

  • Status changed from new to closed
  • Resolution set to invalid

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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.