Sunday, December 1, 2013

Is event handler attached to element?

Not so long ago I had a task to develop a website and as I wouldn't deliver anything less then awesome (such a curse of mine ;) I decided to load every page via ajax. This in combination with HTML5 history API allowed me to provide out-of-this-world-experience for end users so no visible page reloads and what not.

Needles to say I had to deal with event handlers. Attaching these as necessary and making sure same event handler is not attached twice to the same element. So to check what was on .quickPreview I went with standard:

$(".quickPreview").data("events");

.. and to my surprise it didn't work as expected. Instead of Object I got undefined. Not cool so I started googling and it wasn't long till I discovered that above was removed from jQuery 1.8:

$(element).data(“events”): In version 1.6, jQuery separated its internal data from the user’s data to prevent name collisions. However, some people were using the internal undocumented “events” data structure so we made it possible to still retrieve that via .data(). This is now removed in 1.8, but you can still get to the events data for debugging purposes via$._data(element, "events"). Note that this is not a supported public interface; the actual data structures may change incompatibly from version to version.

So the workaround is to use:

$._data($(".getAquoteButton")[0], 'events');

Its definitely good enough for debugging however as per documentation this might change from version to version so I would rather not use it for production code.

No comments:

Post a Comment