views:

613

answers:

2

I'm looking for an updated answer to this question.

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

I know Firebug has a "break on next" button, but there are several mouse listeners on the body element that execute before I can get to the behavior that I want on another particular element, so is there some other way?

+2  A: 

Things are now routed through Element storage : )

Element.getStorage(yourElement).get('prototype_event_registry') will give you an instance of Prototype's Hash, so you can do anything that you would do with 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.

Note that these are undocumented internal details which might be changed in the future, so I wouldn't rely on them except for, perhaps, debugging purposes.

kangax
+1 Thx, kangax. Although you were first, crescentfish gave a more complete answer with the 1.6.0.X version that I needed.
Keith Bentrup
+4  A: 

I've update the answer you linked to with more comprehensive Prototype coverage accounting for changes in versions 1.6.0 to 1.6.1.

It got very messy in between there, 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!') }"
})
Crescent Fresh
Thx for updating the other answer, too! I'm sure it'll help others who found it like I did through google.
Keith Bentrup