Last Updated: February 25, 2016
·
389
· sgtpooki

Navigating Event Hell

Have you ever had to fix bugs, or perform maintenance on a project that overused jquery events? I have.. and it is painful. I've done this a few times now and figured the following would be helpful to anyone else in the same situation:

    //trying to navigate callback hell.
(function() {
    var originalTriggerFunction = $.fn.trigger;
    $.fn.trigger = function() {
        console.log.apply(console, arguments);
        originalTriggerFunction.apply(this, arguments);
    };
})();

This will provide a curried version of $.fn.trigger that logs each call to the console, and then proceeds with the normal $.fn.trigger functionality.

Pro tip #2: You can use the above format to "duck punch" just about any function.