Killing a request

Killing a request

GregPGregP Posts: 500Questions: 10Answers: 0
edited April 2013 in DataTables 1.9
I have a "details view" combined with a polling application. The code is already in place to stop polling when someone opens a details row.

However...

A race condition can be created whereby the request is sent out, the user clicks an "open details" icon, and then the request returns. When the request returns, the table is re-rendered and therefore the details view is re-closed.

I did a bit of poking around and discovered that if I call .abort() on the XHR object, it will cancel normal (plain ol' $.ajax()) requests, but I"m not sure where the DT API hooks into the returned data or if that will ruin things. Also, I'm not sure how to access the XHR object, because my "oTable" variable (I name it differently, but in the convention of the example code here) is a DT object, not an XHR object.

What I would like to do is this:

[code]
$('#wrapper').on('click', '.details_button', function(e) {
e.preventDefault();
xhr.abort(); // I do not actually know how to grab the xhr object, though
do_the_rest_of_my_stuff();
});
[/code]

If I can dynamically get the DT object and then get the XHR object from within, that would be the best route (ie. using 'this' to navigate to the DT object). But I don't mind doing it with my oTable variable, either.

Thanks!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The default fnServerData saves the jQuery XHR object to fnSettings().jqXHR - you could access abort() there.

    Allan
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Cool, thanks Allan.
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited April 2013
    D'oh, found it... I was having some issues, but it's because in my fnServerData I was not adding the ajax call to oSettings, I was just making the call.

    Before:

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    $.ajax( {
    //etc
    })
    [/code]

    After:

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    oSettings.jqXHR = $.ajax( {
    // etc
    })
    [/code]
This discussion has been closed.