PDA

View Full Version : Order of event listeners



grubi
10 Oct 2009, 2:20 AM
Hi.

Is there a supported way to influence the order in which event listeners are fired?
From the docs I only found the 'on' / 'addListener' methods which always append listeners after already existing ones. IMHO it would be great to have at least a method which inserts a listener before any existing ones.

Due to the nature of JS it is no problem to access the 'listeners' array of the event object directly but I'm looking for a supported way which will not eventually break in the future.

Regards,
grubi.

Condor
10 Oct 2009, 2:50 AM
Sorry, the public API currently does not provide a way to add listeners before existing listeners.

Can you describe why you need this feature? I never had a need for it...

grubi
10 Oct 2009, 5:26 AM
Sorry, the public API currently does not provide a way to add listeners before existing listeners.

Can you describe why you need this feature? I never had a need for it...

The cases where this comes into play are rare however sometimes you want to add an event handler which initiates some changes which makes already existent envent handlers to behave different. Sure you can always modify your application to change the order in which event handlers are added but on larger apps this sometimes means to break a clean oop design.

Personally I would add this feature by adding an optional 'priority' parameter to the addListener method. The priority is a simple numeric value which is taken into account when adding it to the listeners array. Listeners without priority will get a default priority assigned.

A quite simple change which will not break existing code and gives you the flexibility to position event handler on application needs.

Just my two cents

Animal
10 Oct 2009, 5:57 AM
Could be that you should be overriding a template method. Are you creating a subclass or a plugin?

grubi
10 Oct 2009, 6:51 AM
Could be that you should be overriding a template method. Are you creating a subclass or a plugin?

Currently I'm creating an end user application. However the architecture is modular and I'm trying to reach a high grade of encapsulation so the modules can be developed and tested outside of the application context. Due to this aspect modules sometimes subscribe to eventhandlers on other modules. Currently I had two cases where a different order of event triggering compared to the order the modules register themselve into their environmental context was desirable.

Not a big deal for me to solve the problem (extjs sources are really well structured) however I was interrested to see if others have similar demands.

Regards,
grubi.

Mike Robinson
12 Oct 2009, 9:46 AM
I happen to use Saki's "msgbus" plugin (http://blog.extjs.eu/plugins/ext-ux-msgbus-plugin/) quite a lot.

The idea is that the existing object's listener is changed so that it "publishes" an event with a very specific event-name, of an agreed-upon application-specific general pattern, which other interested parties can "subscribe" to.

This helps you to more truly isolate the various components of your system, avoiding the need to "look in a particular place" to notice that something has happened (which is a very evil form of coupling...).


The party that is in a natural position to "notice that something has happened" publishes an event to that effect, giving it a specific event-name.
Any party anywhere that "would be interested" just subscribes to the event, using a regular-expression to select only what's interesting. It does not know or care where the event came from.

grubi
12 Oct 2009, 11:27 AM
I happen to use Saki's "msgbus" plugin (http://blog.extjs.eu/plugins/ext-ux-msgbus-plugin/) quite a lot.

The idea is that the existing object's listener is changed so that it "publishes" an event with a very specific event-name, of an agreed-upon application-specific general pattern, which other interested parties can "subscribe" to.


Thanks for the link.
Quite interresting.