【问题标题】:Remove AjaxEventBehavior after execution执行后删除 Ajax 事件行为
【发布时间】:2021-03-05 15:11:14
【问题描述】:

我将以下AjaxEventBehavior 添加到特定组件,但实际上将侦听器添加到整个页面。

Behavior b = new AjaxEventBehavior("keydown") {
    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
        // Here I'm adding a AjaxCallListener overriding getPrecondition to only activate if a certain key was pressed. Probably not that important for this example.
    }

    @Override
    protected void onEvent(AjaxRequestTarget target) {
        // Here I remove (effectively the component is replaced with a empty WebMarkupContainer and not set to invisible) the component the behavior was added to. I know it's a bad solution, but there is no real way around it for me.
    }

    @Override
    protected CharSequence getCallbackScript(Component component) {
        // This is where it gets ugly. Instead of rendering the callbackScript for the component it was added to I want to add it to "window". That's why i use "getPage()" instead of "component".
        CharSequence ajaxAttributes = renderAjaxAttributes(getPage());
        return "Wicket.Ajax.ajax(" + ajaxAttributes + ")"
    }
}

现在我的问题是,即使在执行之后事件仍然被注册。因此,如果该事件第二次发生,它会自动转发到“拒绝访问”页面,这可能是因为该事件仍然对旧页面有一些引用。

理论上只在执行后删除事件就足够了,但我就是不知道该怎么做。但也许还有比这更好的选择,将 AjaxEvent 添加到 windowdocument 不会导致此问题。我知道通常您可以将事件添加到页面中,但在这种情况下,我还必须将整个页面添加到另一个 AjaxRequestTarget 才能运行几行 JS。

【问题讨论】:

  • 如何使用事件命名空间“keydown.globalfancy”并使用 jQuery(e.target).off(".globalfancy") 删除前提条件中的侦听器

标签: wicket


【解决方案1】:

您可以在执行后取消注册事件监听器。有几个地方可以这样做:

  • 在 Java 中

    @覆盖 受保护的无效 onEvent(AjaxRequestTarget 目标){ // ... target.appendJavaScript("jQuery(window).off('keydown')"); }

  • 在 JavaScript 中

    • 正如@svenmeier 在前置条件脚本中建议的那样
    • complete/success/failure 脚本中

    jQuery(attrs.c).off(attrs.e);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多