开发者

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

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 at开发者_开发百科tached 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?


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!') }"
})


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜