Process authenticated flag in json from server before letting Datatables process the response?

Process authenticated flag in json from server before letting Datatables process the response?

stodgestodge Posts: 10Questions: 1Answers: 0
edited September 2011 in General
I'm using datatables with Django on the server. I have it all working nicely but one case is causing me grief. If a user logs out from the site in one browser tab (or is logged out due to inactivity) the page using Datatables gives errors. I have a Django view decorator that inserts a flag into the json response indicating if the user is authenticated or not. The javascript in my other pages checks this flag before continuing. But I can't do this with Datatables because I'm using this code to tell the table to update itself:

[code]event_table.fnDraw(true);[/code]

Is there any way to process the json response in my own code before Datatables processes it? Or is there a better way to handle this situation?

Thanks

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    when you call fnDraw with bServerSide true, it will call fnServerData. In that function/callback, you can examine the json results before processing the data (DataTables passes in "fnCallback". so if you avert from calling that, you've avoided the draw routine):

    put this "fnServerData" function in your initializer
    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) {
    if ({your criteria in the json in case of error/redirect}) { your code in case of error }
    else fnCallback(json);
    } );
    },
    [/code]

    see also: http://www.datatables.net/ref#fnServerData
  • stodgestodge Posts: 10Questions: 1Answers: 0
    Awesome - thanks. This worked perfectly.
This discussion has been closed.