Problem:
I want to validate input as user types it, but validation takes a long time
on the server. When validation result for the first input is returned, user
might have caused several more input events to be validated. These
validation requests are pending execution on Wicket Ajax Channel. At this
point I'm only interested in validating the latest input, but before this
happens, all pending validation requests are processed. This is unnecessary
and takes a lot of time so I want to cancel all the pending requests.

Solution:
I did this by using named Channel and clearing all pending requests before
requesting the latest validation. I couldn't find a solution for this
problem, so I'm posting it here. Hopefully this might help someone else and
please give me some feedback if this solution was not a good one.

Thanks,
Antti Mattila

Here's the code:

public class ClearPendingAjaxRequests extends AjaxCallDecorator {
    private final String clearPendingRequestsScript;

    public ClearPendingAjaxRequests(final String channel) {
        clearPendingRequestsScript = "if (typeof
Wicket.channelManager.channels['" + channel + "'] != 'undefined')
Wicket.channelManager.channels['" + channel + "'].callbacks = new Array();";
    }

    @Override
    public CharSequence decorateScript(final CharSequence script) {
        return clearPendingRequestsScript + script;
    }
}

public class UsersInputFieldOnChangeAjaxBehavior extends
AjaxFormComponentUpdatingBehavior {
    public UsersInputFieldOnChangeAjaxBehavior() {
        super("onkeyup");
        setThrottleDelay(Duration.milliseconds(500));
    }

    @Override
    protected IAjaxCallDecorator getAjaxCallDecorator() {
        return new ClearPendingAjaxRequests(getChannelName());
    }

    @Override
    protected String getChannelName() {
        return "ChannelForValidatingThisSpecificInput";
    }

    @Override
    protected void onUpdate(final AjaxRequestTarget target) {
        // Validate user's input, this takes a long time.
        // Update UI.
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to