I have an API that handles server-side paging, filtering, and sorting. In order to build the dataSource url, I set up transport.read as a function (as described in the documentation). This works fine, except that my API is called twice: once during the initial ajax request, and then again after options.success(result) is called. Anyone else run into this? What exactly does options.success() do that causes another XHR GET request when it already has the data it needs?
UPDATE:
Thanks to Rick's comment below, I did what I should have done this morning and start commenting other kendo grid config code out to see what would happen. Turns out the function that executes when the dataSource is changed is causing the second API call. This is needed so when the grid is sorted, the URL sent to the API contains the updated sorting request. I guess now I'm confused why the change event fires on the initial load when I'm already setting up the default sort parameters. Updated my config below.
Here's my dataSource transport config:
var initGrid = function (take, skip, currPage, field, direction, model, gridElement, cols) {
//set up dataSource
var dataSource = new kendo.data.DataSource({
transport: {
read: function (options) {
$.ajax({
url: apiUrl + '?take=' + take + '&skip=' + skip + '&page=' + currPage + '&pageSize=' + take + '&sort%5B0%5D%5Bfield%5D=' + field + '&sort%5B0%5D%5Bdir%5D=' + direction,
dataType: 'json',
success: function (result) {
// by the time this is reached, the API has already been called once
// the result variable contains the necessary data
options.success(result); // the API is called again when this line executes
},
error: function (result) {
options.error(result);
}
});
}
},
change: function () {
var currentSorting = JSON.stringify(this.sort());
if (currentSorting != sorting) {
sorting = currentSorting;
this.sort(JSON.parse(sorting));
}
field = JSON.parse(sorting)[0].field;
direction = JSON.parse(sorting)[0].dir;
},
sort: { field: field, dir: direction }, //default sort
....
}
}