Wicket 6.1 AjaxEventBehavior - how to set a delay?

AjaxEventBehavior behavior = new AjaxEventBehavior("keyup"){ @Override protected void onEvent(AjaxRequestTarget target) { System.out.println("Hello world!"); } }; form.add(behavior); 

In previous versions of Wicket, I could do it like this:

 behavior.setThrottleDelay(Duration.ONE_SECOND); 

But since version 6.1 this feature has been erased. And the Internet is full of legacy tutorials that contain .setThrottleDelay () methods.

Basically, the goal is to trigger behavior when a person has stopped typing in a form. Currently, it causes behavior every time INSTANTLY, when a key arises, which basically drops out on the server side. That is why I would like to postpone. Prerequisites: I am currently trying to query the database and get data similar to form input. And all this at a time when a person is typing. But a delay would be necessary in order to keep the server side / SQL out of the "bombardment range".

I am also open to alternatives.

+6
source share
3 answers

The throttle settings were unified with all other Ajax settings in AjaxRequestAttributes for version 6.0.0, which is the main version and was not a replacement.

https://cwiki.apache.org/confluence/display/WICKET/Wicket+Ajax contains a table with all the settings, throttling is indicated at the bottom of it.

To use it:

 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) ); } }; 

The last argument to the constructor is what you need. Check out his javadoc.

+9
source

AjaxRequestAttributes looking at the sources, you can get AjaxRequestAttributes via getAttributes() and call setThrottlingSettings() on this.

It is strange that the api change is not mentioned on the wiki. The declaration for 6.1 calls this a replacement.

0
source

Drop behavior seems to be what you need:

dropping - only the last Ajax request is processed, all previously scheduled requests are discarded

You can specify the transition behavior that will be available only for the Ajax channel by setting the AjaxRequestAttributes behavior with AjaxChannel.DROP using updateAjaxAttributes , as stated in the wiki :

 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); 

As @bert also suggested, you can also setThrottlingSettings in AjaxRequestAttributes .

Probably a combination of both behaviors will be better suited to what you think.

0
source

Source: https://habr.com/ru/post/927826/


All Articles