You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Antti Mattila <an...@ri.fi> on 2009/05/13 17:01:42 UTC

Cancelling pending Ajax requests

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Cancelling pending Ajax requests

Posted by Antti Mattila <an...@ri.fi>.
What kind of behavior do you have now and what kind do you want?

When there is 500ms throttle and handling each throttled request takes  
few seconds, you end up queuing and handling each request. If you are  
interested in only the last request, this solution clears the pending  
requests so server gets less requests and the last request gets  
handled faster.

If your throttled request gets handled before the next request, this  
solution doesn't bring any new behavior.

Quoting JohannesK <jo...@eficode.fi>:
>
> Does this solution actually work though? I tried it and I don't see any
> behaviour I wouldn't get from just the throttle delay. Is there something I
> need to change in this to use it? Possibly the channel names?
>
>
> Antti Mattila wrote:
>>
>> 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: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>
>
> --
> View this message in context:   
> http://www.nabble.com/Cancelling-pending-Ajax-requests-tp23523655p23810657.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Cancelling pending Ajax requests

Posted by JohannesK <jo...@eficode.fi>.
Does this solution actually work though? I tried it and I don't see any
behaviour I wouldn't get from just the throttle delay. Is there something I
need to change in this to use it? Possibly the channel names?


Antti Mattila wrote:
> 
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Cancelling-pending-Ajax-requests-tp23523655p23810657.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Cancelling pending Ajax requests

Posted by JohannesK <jo...@eficode.fi>.
Ideally, I would want to add a small wait period after each key stroke before
the event is actually triggered and throttle the last event if another one
comes in before the time period expires. 


Antti Mattila wrote:
> 
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Cancelling-pending-Ajax-requests-tp23523655p23813842.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org