Cancel the table draw when error or '204 No content' response is received
Cancel the table draw when error or '204 No content' response is received
manikanta
Posts: 7Questions: 0Answers: 0
Hi,
I m using DataTables in my new project which is backed by Backbone.js, and so obviously the datatable should be server-side driven.
I've customized the fnServerSide() and it is working quite fine. I've also implemented few CRUD operations like ADD, UPDATE, DELETE.
My implementation flow goes like this - for ex, DELETE:
1. Clicking 'Delete', it will make a call to fnDraw() which in turn calls fnServerData() (as the datatable is server-side driven: bServerSide: true). Before calling the fnDraw(), I m pushing the action details (like the action type, DELETE here, and the action related data, like the row id) into some variable (I couldn't find a better solution :( )
2. In fnServerSide(), it gets the action data from the above variable (using the variable like queue) and gets/modifies the data from Backbone collections instead of directly contacting the server. Backbone' fetch() will do the actual server calls. Here itself I m customizing the pagination meta as required by DataTables.
3. Once the Backbone' fetch/create/destroy/save methods return, I m calling the fnServerData' callback function to report the table that process has completed and now it will comes out from processing state (where the table freezes).
For any of these options, if the service returns some error (like 403, 404, 405) or '204 No content' (when the record detailed info is not available) type response, I want to cancel the table draw so that the existing data will be as it was. Otherwise I need to give the previous data to the datatable (which actually is not that difficult as the Backbone' collection has that data; but I want to stop the unnecessary processing again).
fnPreDrawCallback() seems not the correct function to use for this requirement or is it the correct function? Please suggest.
Thanks,
ManiKanta G
I m using DataTables in my new project which is backed by Backbone.js, and so obviously the datatable should be server-side driven.
I've customized the fnServerSide() and it is working quite fine. I've also implemented few CRUD operations like ADD, UPDATE, DELETE.
My implementation flow goes like this - for ex, DELETE:
1. Clicking 'Delete', it will make a call to fnDraw() which in turn calls fnServerData() (as the datatable is server-side driven: bServerSide: true). Before calling the fnDraw(), I m pushing the action details (like the action type, DELETE here, and the action related data, like the row id) into some variable (I couldn't find a better solution :( )
2. In fnServerSide(), it gets the action data from the above variable (using the variable like queue) and gets/modifies the data from Backbone collections instead of directly contacting the server. Backbone' fetch() will do the actual server calls. Here itself I m customizing the pagination meta as required by DataTables.
3. Once the Backbone' fetch/create/destroy/save methods return, I m calling the fnServerData' callback function to report the table that process has completed and now it will comes out from processing state (where the table freezes).
For any of these options, if the service returns some error (like 403, 404, 405) or '204 No content' (when the record detailed info is not available) type response, I want to cancel the table draw so that the existing data will be as it was. Otherwise I need to give the previous data to the datatable (which actually is not that difficult as the Backbone' collection has that data; but I want to stop the unnecessary processing again).
fnPreDrawCallback() seems not the correct function to use for this requirement or is it the correct function? Please suggest.
Thanks,
ManiKanta G
This discussion has been closed.
Replies
The Ajax handler that DataTables uses internally, just uses the jQuery 'success' callback - so any 2xx return will result in that being called. So what you need to do I think, is to override the way DataTables makes the Ajax call ( http://datatables.net/ref#fnServerData ) and put in a check for the 204 return status code. If you find that, then simply don't call the draw callback function, otherwise do and give it the data (note I've not actually tried this, but I think it should work!).
Allan
[quote]allan said: If you find that, then simply don't call the draw callback function, otherwise do and give it the data[/quote]
If I didn't call the callback fn, the table is stays in freeze state (I mean, in still processing)
ManiKanta
[quote]allan said: The alternative would be to "clean" up the draw, such as removing the processing box and so on, which is equally possible, it requires knowing what functions to run from the code.[/quote]
Can you elaborate a bit more on this? Do you mean, removing the processing element from DOM using jQuery or JS calls or DataTables has a API method for that? I couldn't find such a method.
ManiKanta
Allan