执行后移除 AjaxEventBehavior
Remove AjaxEventBehavior after execution
我有以下 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 添加到 运行.
您可以在执行后取消注册事件监听器。您可以在几个地方执行此操作:
在Java
@Override
protected void onEvent(AjaxRequestTarget 目标){
// ...
target.appendJavaScript("jQuery(window).off('keydown')");
}
在Java脚本中
- 正如@svenmeier 在前提脚本中所建议的那样
- 在
complete/success/failure
脚本中
jQuery(attrs.c).off(attrs.e);
我有以下 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 添加到 运行.
您可以在执行后取消注册事件监听器。您可以在几个地方执行此操作:
在Java
@Override protected void onEvent(AjaxRequestTarget 目标){ // ... target.appendJavaScript("jQuery(window).off('keydown')"); }
在Java脚本中
- 正如@svenmeier 在前提脚本中所建议的那样
- 在
complete/success/failure
脚本中
jQuery(attrs.c).off(attrs.e);