I've been searching around the net and also on this forum for this topic. There're several post but none of them suffices my expectation. We're using a 3rd party framework which registers event handlers on various elements in the DOM, mostly directly on the element but possibly also on a container (not sure if that's the correct wording, i mean the enclosing element).
Basically i would wish for something like described in this post:
https://forum.jquery.com/topic/event-priorityOnly i wouldn't sort the events when inserting but rather when a event was triggered. As i already mentioned the event handler isn't always on the element itself and in case all click events should be prevented i need to ensure to catch them all.
My idea was to overwrite the jQuery.event.handle method as i didn't expect jQuery to change it's code as it kinda breaks the idea of event bubbling. When i came back to look at the internals again i saw handle being deprecated and dispatch sorting the event handling not by priority but by the selector. As this itself breaks event bubbling in a way when i.e. having an a tag enclosed in a div and registering those 3 handlers:
Handler1 - $(document).on('click', 'a', function....)
Handler2 - $('a').on('click', function....)
Handler3 - $('div').on('click', function...)
When clicking on the a tag (without cancelling bubbling of course) the order the handler's would be called using the jQuery.event.dispatch method would be:
Handler2
Handler3
Handler1
I'd say it's arguable if the event order should be the way it is or if the selector of the on method should be taken into account a bit more, as the selector is more specific i'd want the order to be:
Handler2
Handler1
Handler3
Anyways, what i was thinking about was to extend the on method with a priority parameter just like in the post linked above but only sort the event order when this kind of event actually fired. I'd wrap the whole chain, when the event bubbled up all the way i'd resort the handlers and then actually call them based on their priority.
Some more thought has to be put into this as how to prevent resorting the order over and over even if nothing changed.
How can it be accomplished not to sort events without a priority (meaning several handlers are registered in the chain but none of them care when it's running).
How to know when bubbling is done, if no handler is registered on the top DOM element the jQuery.event.dispatch never gets called for this element and there's no way to find out if there're more handler in the chain, which is necessary to know as when all got called they need to be resorted and executed.
I think it would be very beneficial for jQuery to offer such functionality as there're plenty of requests and situations where it would make everybodies life easier.
We've been dealing with Prototype and jQuery in the project for a long time, event handling is the main reason we finally fully switched over to jQuery as Prototype doesn't allow resorting at all (except unregistering all, inserting a new one and reregister the old ones).
Hoping for some input if there's a chance we see such a functionality cooked into jQuery or if we have to go down the lonely road and create our custom plugin?