There is a possible option based on this site question in English (and specifically in the responses of dowski and Avram Lavinsky ), using custom events, the metadata that jQuery provides with $._data()
, and knowing that the events are executed in the order in which they were associated with the element.
The idea (which would only work with the events associated with jQuery) would be as follows:
At first glance it seems simple, right? And the code would not be very complex either:
// asociamos un event listener con el elemento
// MUY IMPORTANTE: esto debe ejecutarse antes de que cualquier otro event listener se añada
$(elemento).on("click", function(e) {
if (e) {
// prevenimos la propagación del resto de eventos click
e.stopImmediatePropagation();
// obtenemos una lista con las acciones asociadas al evento click
var lectoresEvento = $._data(this, "events").click;
// para cada elemento de la lista
for (var x = 0; x < aClickListeners.length; x++) {
// ejecutar la función
aClickListeners[x].handler();
}
// invocar el evento personalizado afterclick
$(this).trigger("afterclick")
}
});
The idea would be to run that code in your library, after loading jQuery and before loading any other code. Later you can associate (or not) the event afterclick
to the different elements.
The custom event will always run at the end of all clicks; and if there is none, it will be executed anyway (something like the finally
in the exceptions).
Advantages of this method:
- It works: P
- You do not need to know how many event handlers have been created, nor their name. All will appear sorted in the list of
$._data()
.
Disadvantages of this method:
- Only works within the jQuery universe.
-
$._data(this, "events")
does not work in versions prior to 1.8, for those you would have to modify the code a bit and use element.data('events')
(as explained in this question from the English site).
Here is an example (you can see the results in the console):
// Asociamos un evento click a todos los elementos
$("*").on("click", function(e) {
if (e) {
e.stopImmediatePropagation();
var lectoresClick = $._data(this, "events").click;
for (var x = 0; x < lectoresClick.length; x++) {
lectoresClick[x].handler();
}
$(this).trigger("afterclick");
}
});
// definimos las acciones que se ejecutarán después de todas las acciones del evento click
$("button").on("afterclick", function() {
console.log("Ejecutados todos los eventos click de " + $(this).attr("id"));
});
// asociamos diferentes acciones al evento click de los botones
$("#Boton1").on("click", function() { console.log("Primer evento botón 1"); });
$("#Boton2").on("click", function() { console.log("Primer (y único) evento botón 2"); });
$("#Boton1").on("click", function() { console.log("Segundo evento botón 1"); });
$("#Boton1").on("click", function() { console.log("Tercer evento botón 1"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="Boton1">Botón 1</button>
<button id="Boton2">Botón 2</button>
<button id="Boton3">Botón 3</button>