This is a migrated thread and some comments may be shown as answers.

[Solved] Cancel Grid Ajax Request

4 Answers 105 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 23 Jan 2012, 04:50 PM
We are using a previous release of Telerik MVC, from March 2011 in our web app. It's working fine for us, but I'm looking at upgrading to the newer release to get some of the new features. However, I have a snag.

We have various user actions that result in the grid control fetching new data from the server. As the user actions can sometimes outpace the particular query that happens, we thought it would be good to cancel the pending requests. We call $grid.ajaxRequest() manually to get the grid to update; and in the March version this meant a simple change - we modified grid.ajaxRequest to return the Jquery XHR object that was being used for the request, and handled a simple "do I have an outstanding request still running? Ok cancel that one then issue this new one" pattern. I didn't see a way to get that behavior otherwise and we haven't had any problems so it seemed fine.

So I'm looking at the November 2011 version of Grid and Common, and the new DataSource / Query / Transport pattern has me a bit stymied. I don't see an easy change to make to get the active XHR from a call to ajaxRequest on grid - am I missing something obvious? Is there a better way, or a built in API call I'm overlooking, by which I can accomplish a cancellation of a grid refresh?

4 Answers, 1 is accepted

Sort by
0
Dickie
Top achievements
Rank 1
answered on 03 May 2012, 08:13 PM
I have a similar scenario and was wondering if you ever found anything about this.
0
Mike
Top achievements
Rank 1
answered on 03 May 2012, 09:21 PM
No I have not. Other pressing items were more important than upgrading so we are still sitting on the old release. I will certainly post here if I ever figure something out though.
0
Accepted
Denis
Top achievements
Rank 1
answered on 11 May 2012, 08:07 PM
I found a way!

You can use the $.ajaxPrefilter for jQuery to cancel any ajax request.

jQuery.ajaxPrefilter

http://api.jquery.com/jQuery.ajaxPrefilter/


var currentRequests = {};

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    if (options.url.indexOf("youurltocancel") > 0
        || options.url.indexOf("youotherurltocancel") > 0) {
        if (currentRequests[options.url]) {
            currentRequests[options.url].abort();
        }
        currentRequests[options.url] = jqXHR;
    }
});
0
Mike
Top achievements
Rank 1
answered on 11 May 2012, 08:17 PM
That's wonderfully clever. Thanks for sharing!
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Dickie
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Denis
Top achievements
Rank 1
Share this question
or