有没有办法找回在Prototype中使用Event.observe注册的匿名事件处理程序?

gct*_*gct 0 javascript events prototypejs

与遗留代码接口,我有这样的东西:

Event.observe(some_form, 'submit', [some anonymous function])
Run Code Online (Sandbox Code Playgroud)

我想抓住那个匿名事件处理程序,在Prototype中有一个简单的方法吗?

Cre*_*esh 5

这取决于Prototype的版本.从我之前写的更一般的答案:

  • 版本1.5.x:

    // inspect
    Event.observers.each(function(item) {
        if(item[0] == some_form && item[1] == 'submit') {
            alert(item[2]) // [some anonymous function]
        }
    })
    
    Run Code Online (Sandbox Code Playgroud)
  • 版本1.6到1.6.0.3,包括在内(这里非常困难)

    // inspect. "_eventId" is for < 1.6.0.3 while 
    // "_prototypeEventID" was introduced in 1.6.0.3
    var submitEvents = Event.cache[some_form._eventId || (some_form._prototypeEventID || [])[0]].submit;
    submitEvents.each(function(wrapper){
        alert(wrapper.handler) // [some anonymous function]
    })
    
    Run Code Online (Sandbox Code Playgroud)
  • [当前]版本1.6.1(好一点)

    // inspect
    var submitEvents = some_form.getStorage().get('prototype_event_registry').get('submit');
    submitEvents.each(function(wrapper){
        alert(wrapper.handler) // [some anonymous function]
    })
    
    Run Code Online (Sandbox Code Playgroud)