Java Wicket 6.1 AjaxEventBehavior-如何设置延迟?
在以前版本的Wicket中,我可以这样做:Java Wicket 6.1 AjaxEventBehavior-如何设置延迟?,java,ajax,wicket,Java,Ajax,Wicket,在以前版本的Wicket中,我可以这样做: AjaxEventBehavior behavior = new AjaxEventBehavior("keyup"){ @Override protected void onEvent(AjaxRequestTarget target) { System.out.println("Hello world!"); } }; form.add(behavior); 但从6.1版开始,这个机会就消失了。we
AjaxEventBehavior behavior = new AjaxEventBehavior("keyup"){
@Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println("Hello world!");
}
};
form.add(behavior);
但从6.1版开始,这个机会就消失了。web上充满了以前版本的教程,它们都包含.setThrottleDelay()方法
基本上,目标是在用户停止在表单中键入内容时调用该行为。目前,每次当密钥出现时,它都会立即调用该行为,这基本上是向服务器端发送垃圾邮件。这就是我想推迟的原因。背景:我目前正在尝试对数据库进行查询,并获取与表单输入类似的数据。所有这些都发生在这个人打字的时候。但为了使服务器端/SQL不受“轰炸范围”的影响,需要延迟
另外,我对其他选择持开放态度。查看源代码,您似乎可以通过
getAttributes()
获取AjaxRequestAttributes
,并在此基础上调用setThrottlingSettings()
奇怪的是,维基中没有提到api的变化。6.1的发布将其称为替代品。这似乎就是您所追求的:
删除-只处理最后一个Ajax请求,所有这些都是以前处理的
计划的请求将被丢弃
您可以指定一个drop行为,该行为仅适用于Ajax通道,方法是使用自定义该行为的类型,如中所述:
正如@bert所建议的,您还可以访问AjaxRequestAttributes
这两种行为的组合可能更适合您的需要。设置节流阀的设置已与AjaxRequestAttributes for version 6.0.0中的所有其他Ajax设置统一,该版本是一个主要版本,不是替代版本 包含一个包含所有设置的表,节流设置在表的底部 要使用它:
AjaxEventBehavior behavior = new AjaxEventBehavior("keyup"){
@Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println("Hello world!");
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
super.updateAjaxAttributes(attributes);
attributes.setChannel(new AjaxChannel("myChannel", AjaxChannel.Type.DROP));
}
};
form.add(behavior);
最后一个构造函数参数就是您所需要的。检查它的javadoc getAttributes()是AbstractDefaultAjaxBehavior的最后一个方法,我不能重写它。通过阅读源代码,我还通过AjaxFormValidatingBehavior找到了setThrottlingSettings,但在过去和现在,它也给了我一个错误(覆盖问题)。你是对的,应该启用覆盖,正如在中指出的。我已经更新了answerDROP无法解析或不是字段。即使AjaxChanne.Type显示“DROP”
@Override protected void updateAjaxAttributes(AjaxRequestAttributes属性){ThrottlingSettings ThrottlingSettings=new ThrottlingSettings(“throttling”,持续时间。1秒);attributes.setThrottlingSettings(ThrottlingSettings);attributes.setChannel(AjaxChannel.DROP);}
现在当我在表单中获得keyup时,然后它等待一秒钟,并给出输出“HelloWorld!”,如果我再次键入,它将不会再这样做。非常抱歉,哈维打扰了您,但在这个问题上,网络上真的是空荡荡的实际上,您需要使用其构造函数构造一个新的AjaxChannel
。提供名称并AjaxChannel.Type.DROP
。我也很抱歉,我从未使用过这个(用于覆盖AjaxBehavior的getChannelName()
以返回Wicket 1.3中的0 | d
),也没有立即测试代码是否正常工作。谢谢,Xavi,现在有了这个错误,一切正常。但是,对于只调用一次OneEvent()的问题,还有什么进一步的想法吗?6.0.0版已经做了这个更改,在(Ajax的扩展迁移页面)中有描述。@martin-g:谢谢。OP写到它是在6.1中引入的(这会很奇怪),我没有检查。感谢您澄清这一点。您的代码示例将“id”作为ThrottlingSettings的第一个参数传递-这个变量是什么?我还对“id”参数感到困惑!!??来自Javadoc:客户端节流代码使用该id跟踪各种事件节流。通常,您可以在此处使用任何唯一ID,例如组件的markupId(WebComponent#getMarkupId()
)。若要将多个不同的事件合并到一个节流阀,请为它们指定相同的ID。如果您没有明确设置节流阀ID,Wicket 7会自动为您设置组件标记ID。请检查您的代码,似乎缺少{
。
AjaxEventBehavior behavior = new AjaxEventBehavior("keyup"){
@Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println("Hello world!");
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
super.updateAjaxAttributes(attributes);
attributes.setChannel(new AjaxChannel("myChannel", AjaxChannel.Type.DROP));
}
};
form.add(behavior);
AjaxEventBehavior behavior = new AjaxEventBehavior("keyup") {
@Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println("Hello world!");
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
super.updateAjaxAttributes(attributes);
attributes.setThrottlingSettings(
new ThrottlingSettings(id, Duration.ONE_SECOND, true)
);
}
};