Update. How to find event listeners on a DOM node in a prototype?

I am looking for an updated answer to this question .

It seems that Event.observers is no longer used (possibly to avoid memory leaks) in Prototype 1.6+, so how now to track down which event listeners are bound to an element?

I know that Firebug has a โ€œbreak on nextโ€ button, but there are several mouse listeners in the body element that are executed before I can move on to the behavior I want to use for another specific element, is there also another way?

+6
javascript prototypejs events listener observer-pattern
source share
2 answers

I updated the answer that you linked to with a more comprehensive Prototype coverage that takes into account changes from version 1.6.0 to 1.6.1 .

It was very dirty between them, but 1.6.1 is somewhat clean:

 var handler = function() { alert('clicked!') }; $(element).observe('click', handler); // inspect var clickEvents = element.getStorage().get('prototype_event_registry').get('click'); clickEvents.each(function(wrapper){ alert(wrapper.handler) // alerts "function() { alert('clicked!') }" }) 
+7
source share

Things are now routed through the item store :)

Element.getStorage(yourElement).get('prototype_event_registry') will provide you with a Prototype Hash instance, so you can do whatever you would do with the hash.

 // to see which event types are being observed Element.getStorage(yourElement).get('prototype_event_registry').keys(); // to get array of handlers for particular event type Element.getStorage(yourElement).get('prototype_event_registry').get('click'); // to get array of all handlers Element.getStorage(yourElement).get('prototype_event_registry').values(); // etc. 

Please note that this is undocumented internal data that may be changed in the future, so I will not rely on them, except, perhaps, for debugging purposes.

+6
source share

All Articles