Wicket 执行后删除AjaxEventBehavior

Wicket 执行后删除AjaxEventBehavior,wicket,Wicket,我有下面的AjaxEventBehavior,它被添加到特定的组件中,但实际上在整个页面中添加了一个侦听器 Behavior b = new AjaxEventBehavior("keydown") { @Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { // Here I'm adding a AjaxCallListener ov

我有下面的
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添加到
窗口
文档
,这不会导致此问题。我知道通常可以将事件添加到页面,但在这种情况下se我还必须将整个页面添加到另一个AjaxRequestTarget,以便运行几行JS。

您可以在执行后注销事件侦听器。您可以在几个地方执行此操作:

  • 在爪哇

    @凌驾 受保护的void onEvent(AjaxRequestTarget目标){ // ... appendJavaScript(“jQuery(window.off('keydown')”); }

  • 在JavaScript中

    • 正如@svenmeier在前提条件脚本中建议的那样
    • complete/success/failure
      脚本中
    jQuery(attrs.c).off(attrs.e)


使用事件名称空间“keydown.globalvancy”并使用jQuery(e.target.off)(“globalvancy”)删除前置条件中的侦听器怎么样