rapid serverside queries issue
rapid serverside queries issue
We have a knockout observable string that's tied to a search text field. If we don't use notifyWhenChangesStop of 500ms, when user types text in the field, the api.draw() command is called rapidly in succession.
Example:
user types in the following text: 1020
each character fires the api.draw() command.
The correct list of rows is being rendered (one matching result in this example)
but when the user click on the column to edit the associated record, the array of data listed in our viewmodel is from one of the previous queries (1, 10, 102, but not from the 1020 final query).
So when we try to retrieve the record based on the 'index' in the visually rendered table rows (which is index 0 in this case),
the array that holds the results of the query (this.data) has a list of 10 results, the result at index 0 is not the one that we see in the table (in fact there should only be 1 result in the this.data array which is returned from the server and fed into the datatables renderCallback).
I'm assuming that this 'may' happen because the rapid requests to the server (4 in this case) may be coming back out of order, and something in datatables may be getting a bit confused.
This all sounds 'impossible' to me but it's happening.
Any ideas about what may be going on here?
// get data from server
this.getTableData()
.then(() => {
// feed data back into DataTable
callback({
draw: <number>data.draw, // this type coersion was recommended by DataTables.net for security reasons
data: this.data,
recordsTotal: this.dataCount,
recordsFiltered: this.dataCount
});
this.updateUrl();
});
The getTableData() method sends the ajax request to our server and the response is set in the this.data and this.dataCount fields before the promise is .then continued.
Answers
The
draw
parameter is what should be making this impossible. As long as the sequence is triggered correctly from the client-side there should be no way for DataTables to draw them out of order due to thedraw
parameter (which was added for example that reason).Is the draw parameter actually being sent to and then returned from the server? It looks like it might getting coerced on the client-side there?
Allan
the draw parameter is not being sent to/returned from the server.. I was assuming that the promise chain would manage the request/response and not mix them.
You're saying I need to handle this on the server, and return the draw number in the response and then 'validate' that it's the same draw number sent to the server that's then received from the server?
This part of DataTables was written long before promises were introduced into Javascript. However, you are correct, this is something that should be handled on the client-side and it will be in the next major version of DataTables.
Until then, yes, you need the server to return what is basically a sequence number - which DataTables uses to make sure that they are not drawn out of sequence.
Allan