PrototypeJS事件注册问题

15

在更新到版本 1.7.3 后,原型事件注册表似乎存在问题。我正在使用 prototype_event_registry 访问元素存储上的点击事件,以便我可以重放它们。

这样我就可以停止事件并根据回调选择性地恢复它们。一切都运行良好,但在查看 1.7.01.7.3 差异后,它似乎已经被移除了?

我知道这是内部实现,可能不应该在第一时间使用它。无论如何,我的问题是:

我已将代码更新为与 1.7.3 兼容,但对我来说似乎非常卑劣,是否有更好的方法来做到这一点?

/**
 * creates a toggling handler for click events taking previous click events into account.
 *
 * w.r.t stopping of a click event, handles cases where the button is a submit or a normal button.
 * in the case of a submit, calling <tt>Event.stop()</tt> should be sufficient as there are no other listeners on the button.
 * however, if a normal button has a handler that calls <tt>save()</tt>, and another handler using client code and calling stop,
 * it will not affect stopping of the event, since <tt>Event.stop</tt> only stops propagation, not other handlers!
 *
 * note that this function will always execute the specified handler before any other defined events.
 *
 * @param {Element} element the element to use for this click event
 * @param {Function} handler the handler to use for this stopping click event, if this handler returns true,
 * all other actions for the click event will be prevented
 * @returns {Element} the element that was supplied as argument
 */
function stoppingClickEvent(element, handler) {
    if (!element) throw 'cannot use method, if element is undefined';

    // assign default handler if none was supplied
    handler = handler || Prototype.emptyFunction;

    if (element.type && element.type === 'submit') {
        element.on('click', function(submitEvent) {
            // call the supplied handler with the current element as context and the event as argument
            // if it returns true, we should stop the event
            var stopEvent = handler.call(element, submitEvent);

            if (stopEvent) {
                // since the element's default action will be to submit a form, we prevent it
                submitEvent.stop();
            }
        });
    } else {
        // prototype 1.7.3 removed support for 'prototype_event_registry', so we need to do multiple hacks here
        // first get the window of the element so we can access the prototype
        // event cache from the correct context (frames)
        var elementDoc = element.ownerDocument;
        var elementWindow = elementDoc.defaultView || elementDoc.parentWindow;

        if (!elementWindow) {
            throw 'cannot access the window object for element ' + element.id;
        }

        // we are dealing with a normal element's click event, so we don't know how many click events have been set up.
        // capture them all so we can decide to call them or not.
        // FIXME: need a better way of doing this
        var registry = elementWindow['Event'].cache[element._prototypeUID || element.uniqueID] || {},
            events = registry['click'] || [];

        // now that we have a copy of the events, we can stop them all and add our new handler
        element.stopObserving('click').on('click', function(clickEvent) {
            // call the supplied handler with the current element as context and the event as argument
            // if it returns true, we should stop the event
            var stopEvent = handler.call(element, clickEvent);

            if (!stopEvent) {
                // the event should not be stopped, run all the original click events
                events.each(function(wrapper) {
                    wrapper.handler.call(element, clickEvent);
                });
            }
        });
    }

    return element;
}

1
看起来事件注册表在1.7.1中被重写了——Event.cache对象被设置为防止一些内存泄漏并提高性能。我非常确定任何扩展元素上都应该存在element._prototypeUID,但除此之外,这将是引用每个元素的事件注册表的方式。 - Geek Num 88
Event.stop(e)难道不是在执行它应该做的事情吗?它应该停止事件传播,不是吗? - Kiran Shakya
@kiran,它可以停止事件冒泡,但不会停止在按钮上指定的任何其他单击处理程序,因此需要这个函数 :) - epoch
1个回答

1
在运行以上代码3-4个月后,我最终决定还原它。似乎存在很多问题,特别是在处理单个页面上的多个框架和事件处理程序时。
最普遍的问题是,特定元素的Event.cacheundefined
这可能是由于上述不正确的处理,但我非常怀疑新的Event框架在某些方面是不正确的,因为还原到1.7.0完全解决了我遇到的所有问题。
仅供参考,这是我现在使用的1.7.0代码:
/**
 * creates a toggling handler for click events taking previous click events into account.
 *
 * w.r.t stopping of a click event, handles cases where the button is a submit or a normal button.
 * in the case of a submit, calling <tt>Event.stop()</tt> should be sufficient as there are no other listeners on the button.
 * however, if a normal button has a handler that calls <tt>save()</tt>, and another handler using client code and calling stop,
 * it will not affect stopping of the event, since <tt>Event.stop</tt> only stops propagation, not other handlers!
 *
 * note that this function will always execute the specified handler before any other defined events.
 *
 * @param {Element} element the element to use for this click event
 * @param {Function} handler the handler to use for this stopping click event, if this handler returns true,
 * all other actions for the click event will be prevented
 * @returns {Element} the element that was supplied as argument
 */
function stoppingClickEvent(element, handler) {
    if (!element) throw 'cannot use method, if element is undefined';

    // assign default handler if none was supplied
    handler = handler || Prototype.emptyFunction;

    if (element.type && element.type === 'submit') {
        element.on('click', function(submitEvent) {
            // call the supplied handler with the current element as context and the event as argument
            // if it returns true, we should stop the event
            var stopEvent = handler.call(element, submitEvent);

            if (stopEvent) {
                // since the element's default action will be to submit a form, we prevent it
                submitEvent.stop();
            }
        });
    } else {
        // we are dealing with a normal element's click event, so we don't know how many click events have been set up.
        // capture them all so we can decide to call them or not.
        var registry = element.getStorage().get('prototype_event_registry') || $H(),
            events = registry.get('click') || [];

        // now that we have a copy of the events, we can stop them all and add our new handler
        element.stopObserving('click').on('click', function(clickEvent) {
            // call the supplied handler with the current element as context and the event as argument
            // if it returns true, we should stop the event
            var stopEvent = handler.call(element, clickEvent);

            if (!stopEvent) {
                // the event should not be stopped, run all the original click events
                events.each(function(func) {
                    func.call(element, clickEvent);
                });
            }
        });
    }

    return element;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接