[jQuery] Re: Event namespacing - how to see the full event name?
You're allowed to pass in an array object along with each trigger method call. So this might work for you: var call = 1; $('.foo').trigger('bar.update',[call]]); var call = 2; $('.foo').trigger('bar.update',[call]]); $('.foo').bind('bar', function(e,data) { if (data[0] == 1) { // I came from call 1 } else { // i came from call 2 } }); _ From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Guy Fraser Sent: Tuesday, March 25, 2008 1:34 PM To: jquery-en@googlegroups.com Subject: [jQuery] Event namespacing - how to see the full event name? Hi, I've been triggering several events as follows: $('.foo').trigger('bar.update'); $('.foo').trigger('bar.show'); etc... If I listen to the bar event on foo: $('.foo').bind('bar', function(e) { // e.type = 'bar' }); The event type property is always bar - how do I work out if bar.update vs. bar.show was triggered? I might be misunderstanding the event system (or more accurately it's namespacing) but I imagined that I could listen to a specific bar event: $('.foo').bind('bar.update', handler); Or all bar events (bar.update, bar.show, etc): $('.foo').bind('bar', handler); When listening to all of them with a single handler, it would be useful for the handler to see the full event name (bar.update instead of just bar) so I could switch on event type inside the handler. I guess I could achieve the required effect using event data, but it seems such a waste not to have the full event name in the event type (or possibly a .name property could contain it?) Any ideas? Guy
[jQuery] Re: Event namespacing - how to see the full event name?
You're correct about the name-spacing. The name-space comes after the event type: $('.class').bind('click.namespace', function(){//}); $('.class').trigger('click.namespace'); (from: http://docs.jquery.com/Events_%28Guide%29 ) Karl On Wed, Mar 26, 2008 at 5:34 AM, Guy Fraser [EMAIL PROTECTED] wrote: Hi, I've been triggering several events as follows: $('.foo').trigger('bar.update'); $('.foo').trigger('bar.show'); etc... If I listen to the bar event on foo: $('.foo').bind('bar', function(e) { // e.type = 'bar' }); The event type property is always bar - how do I work out if bar.update vs. bar.show was triggered? I might be misunderstanding the event system (or more accurately it's namespacing) but I imagined that I could listen to a specific bar event: $('.foo').bind('bar.update', handler); Or all bar events (bar.update, bar.show, etc): $('.foo').bind('bar', handler); When listening to all of them with a single handler, it would be useful for the handler to see the full event name (bar.update instead of just bar) so I could switch on event type inside the handler. I guess I could achieve the required effect using event data, but it seems such a waste not to have the full event name in the event type (or possibly a .name property could contain it?) Any ideas? Guy
[jQuery] Re: Event namespacing - how to see the full event name?
Yeah, I thought I'd have to use the data feature, it just seems a shame not to have a property on event that provides that info. I did some digging in the jQuery source and found that i's trivial to expose the namespaced event type by inserting the following in to the handle() method before line 2059 in the current public release of 1.2.3: event.namespaced = event.type; With that added, all events then have a .namespaced property on the event parameter allowing you to see exactly which event was called and switch on it :) I've submitted a feature request, including an example scenario and code: http://dev.jquery.com/ticket/2585 Guy Andy Matthews wrote: You're allowed to pass in an array object along with each trigger method call. So this might work for you: var call = 1; $('.foo').trigger('bar.update',[call]]); var call = 2; $('.foo').trigger('bar.update',[call]]); $('.foo').bind('bar', function(e,data) { if (data[0] == 1) { // I came from call 1 } else { // i came from call 2 } });