Bug using Async true and stateLoadCallback

Bug using Async true and stateLoadCallback

lenamtllenamtl Posts: 265Questions: 65Answers: 1
edited August 2020 in Free community support

Hi,

I'm using DataTables 1.10.11 (I cannot upgrade for now)
I'm having an issue with Async true.

The bug was mentionned here
https://datatables.net/forums/discussion/49373#Comment_130295

I'm trying to fix the code manually based with the fixe provided here, but with no luck
https://github.com/DataTables/DataTablesSrc/commit/9c3e8eaa7935eaa5f46f05714ceb6bf2998328f8

The fix does not solved the problem as if I remove "async": false, from my code it won't load the Datatables values.

So is there another fix that I can applied manually, or maybe a better fix was posted?

stateLoadCallback: function(settings) {                     

    var table49 = "allquotes";
    var o;

    $.ajax( {
        "url": "ASEngine/ASAjax.php?action=load",
        "data":{ "user_id":"<?php echo $userId; ?>", "action" : "loadDataTable", "tablename" : table49},
        "async": false, // problem
        "type": "POST",
        "success": function (json) {
                        o = json;
        }
    });

        if(o){
            return JSON.parse(o);
        }else{

    }
}

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Is there any reason you can't update. DataTables should be backward compatible.

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited August 2020

    Yes of course, here my reasons, if you can help for any of these maybe I would be able to update and have the time to tests, at the moment 4) is the main problem.

    or if you can give a solution to fix the old version that would be the faster way for now

    1) the application is almost completed and tested that will required a lot of tests,I have 55 tables with lot of custom code that fix previous issues..

    2) I did an update to test I got 2 warning issues
    https://datatables.net/forums/discussion/63795/pdfmake-warning-failed-to-load-sourcemap

    The main issue
    https://datatables.net/forums/discussion/63794/colreorder-warning-added-non-passive-event-listener-to-a-scroll-blocking

    3) I will need to check if all issues are now fixed or not that can take a lot of time I found several bugs and that took some time to fix them all. I did some test and some of the fix break the actual code for example this old fix

    https://github.com/DataTables/Responsive/issues/90#issuecomment-348272358

    4) The main problem is I don't know how to update my actual stateLoadCallback from this question to work with the new method

    https://datatables.net/forums/discussion/63797/i-need-help-to-update-stateloadcallback-to-new-method#latest

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited August 2020

    Updates

    2) I have fixed pdfmake warning
    2) I have not fixed colreaorder waning

    3) this bug is still present and my old code fix is not working anymore

    4) still looking help to adapt the code

    So I beleive I will have to many fix to go through for now with the recent Datatables and probably to many code to adapt ( I started the process but that would take sonme time)

    So how can I manually fix the Async true DataTables 1.10.11 for now ?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    All I can suggest is to take a look at the fix that Allan made in the thread you link to, and see if you can add that fix into your version of the files.

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited August 2020

    The fix provided
    https://github.com/DataTables/DataTablesSrc/commit/9c3e8eaa7935eaa5f46f05714ceb6bf2998328f8

    does not work on DataTables 1.10.11, What I mean is after applying the fix, if I removed the async false or comment it or use async true = the stateLoadCallback is not loading the data

    plus if you read the user comment he seems to said that the fix was causing issue

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    We don't support old versions, I'm afraid. So you'll either need to upgrade, or retrofit the changes back into your version. It's open source, so you have access to the commit history.

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Ok I understand, I'm in the process to update.

    Could you help me with
    https://datatables.net/forums/discussion/63797/i-need-help-to-update-stateloadcallback-to-new-method

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Hi,

    I'm testing my old function on the latest Datatables
    This is still work ONLY if Async is set to false.
    when it is set to async true this is not working.

    I'm trying to us the new method with the callback, but there is no information on how to use it or parse it...

        stateLoadCallback: function(settings) {                    
    
            var table49 = "allquotes";
            var o;
    
            $.ajax( {
                "url": "ASEngine/ASAjax.php?action=load",
                "data":{ "user_id":"<?php echo $userId; ?>", "action" : "loadDataTable", "tablename" : table49},
                "async": false, // problem
                "type": "POST",
                "success": function (json) {
                                o = json;
                }
            });
    
                if(o){
                    return JSON.parse(o);
                }else{
    
            }
        }
    
  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Since Ajax is an asynchronous process you will need to do your processing of the JSON response in the success function. if you remove line 9 "async": false, and execute the code as is then the if statement in line 16 is executed before the Ajax response, ie, before the success function. Move it inside the success function to have it execute at the proper time.

    Kevin

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Look at the examples in the stateLoadCallback. There is an added callback to versions 1.10.13 and newer. You are not using this structure, from the docs:

    $('#example').DataTable( {
        stateSave: true,
        stateLoadCallback: function (settings, callback) {
            $.ajax( {
                url: '/state_load',
                dataType: 'json',
                success: function (json) {
                    callback( json );
                }
            } );
        }
    } );
    

    Kevin

This discussion has been closed.