st6mm st6mm - 5 months ago 26
Ajax Question

Wicket 6.1 AjaxEventBehavior - how to set 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 have done it like:

behavior.setThrottleDelay(Duration.ONE_SECOND);


But since version 6.1 this opportunity has been erased. And the web is full of previous versions tutorials which all contain .setThrottleDelay() methods.

Basically, the goal is to call out the behavior when the person has stopped typing in the form. Currently it calls out the behaviour every time INSTANTLY when the key gets up which basically spams the server side. That's why I would like to delay. Background: I'm currently trying to make queries into database and get out the data which are similar to the form input. And all that in the time when the person is typing. But the delay would be needed in goal to keep server-side/SQL out of the "bombarding range".

Also I am open to alternatives.

Answer

Setting the throttle's settings has been unified with all other Ajax settings in AjaxRequestAttributes for version 6.0.0 which is a major version and was not drop-in replacement.

https://cwiki.apache.org/confluence/display/WICKET/Wicket+Ajax contains a table with all the settings, the throttling ones are mentioned 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 constructor argument is what you need. Check its javadoc.