Timofey Gorshkov
Timofey Gorshkov

Reputation: 5125

Wicket 1.4 periodic component state update using AJAX

I have a link that should fire some process on the server and go inactive, then it should monitor this process at the background, and when it will be finished, link should be updated. All these actions should be done with the aid of AJAX.

Example of link transformation: Bake a cake -> Baking a cake -> Load baked cake


This workflow could be done within onClick method of AjaxLink, but it will block another AJAX requests, and will go down on long processing time.

Upvotes: 2

Views: 1098

Answers (1)

divanov
divanov

Reputation: 6339

One option here is to use AbstractAjaxTimerBehavior to poll server status regularly and update the label accordingly

/* Create stopped timer */
AbstractAjaxTimerBehavior timer = new AbstractAjaxTimerBehavior(Duration.seconds(1)) {
    @Override
    protected void onTimer(AjaxRequestTarget target) {
        if (serverIsReady()) {
            /* Stop timer */
            this.stop(target);
            /* Update UI */
            label.setDefaultModel("Load baked cake");
            target.add(label);
        }
    }
});
link.add(timer);
timer.stop();

/* Create triggering event behaviour */
link.add(new AjaxEventBehavior("onclick") {
    @Override
    protected void onEvent(AjaxRequestTarget target) {
        /* Update UI */
        label.setDefaultModel("Baking a cake");
        target.add(label);
        /* Start timer */
        timer.restart(target); /* It seems this method doesn't exist in Wicket 1.4 */
    }
});

Upvotes: 6

Related Questions