st6mm
st6mm

Reputation: 239

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.

Upvotes: 7

Views: 5483

Answers (3)

martin-g
martin-g

Reputation: 17513

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.

Upvotes: 11

Xavi López
Xavi López

Reputation: 27880

It seems that drop behavior is what you're after:

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

You can specify a drop behavior, that will only for the Ajax channel by customizing the AjaxRequestAttributes of the behavior with AjaxChannel.DROP by means of updateAjaxAttributes, as pointed out 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 to the AjaxRequestAttributes.

Probably a combination of both behaviors would fit better in what you seem to need.

Upvotes: 0

bert
bert

Reputation: 7696

Looking at the sources, it looks as you can get the AjaxRequestAttributes via getAttributes() and call setThrottlingSettings() on this.

Strange that the api change is not mentioned in the wiki. The announcement for 6.1 calls it an drop in replacement.

Upvotes: 0

Related Questions