【发布时间】: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 添加到 window 或 document 不会导致此问题。我知道通常您可以将事件添加到页面中,但在这种情况下,我还必须将整个页面添加到另一个 AjaxRequestTarget 才能运行几行 JS。
【问题讨论】:
-
如何使用事件命名空间“keydown.globalfancy”并使用 jQuery(e.target).off(".globalfancy") 删除前提条件中的侦听器
标签: wicket