Transport
The transport object contains properties that a DataSource can use to communicate with a remote server for any of its normal data functions. Those data functions are create, destroy, read, and update (corresponding to the different actions that can be taken on a record). Each of these data functions exists as a property object within the transport object and follows the same pattern of confiugration options.
There have different options for configuring the transport object's remote data operations. Each of the four properties follows the same configuration pattern.
Using property value
create: {
// for creating data records on remote source.
url: "/orders/create",
// the url to the create service.
data: {
// data to send to the create service as part of the request.
// this can also be specified as a function call.
orderId: $("#input").val()
},
cache: true,
// make false to force fresh requests every time.
contentType: "application/json",
// default is
// "application/w-www-form-urlencoded"
dataType: "json",
// "jsonp" is also common.
type: "POST"
// which http verb to use.
}
Using function (Passing Remote Data)
var dataSource = new kendo.data.DataSource({
transport: {
read: function(options) {
// make JSONP request to http://demos.telerik.com/kendo-ui/service/products
$.ajax({
url: "http://demos.telerik.com/kendo-ui/service/products",
// "jsonp" is required for cross-domain requests;
// use "json" for same-domain requests
dataType: "jsonp",
success: function(result) {
// notify the data source that the request succeeded
options.success(result);
},
error: function(result) {
// notify the data source that the request failed
options.error(result);
}
});
},
update: function(options) {
alert(1);
// make JSONP request to http://demos.kendoui.com/service/products/update
$.ajax( {
url: "http://demos.kendoui.com/service/products/update",
dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
// send the updated data items as the "models" service parameter encoded in JSON
data: {
models: kendo.stringify(options.data.models)
},
success: function(result) {
// notify the data source that the request succeeded
options.success(result);
},
error: function(result) {
// notify the data source that the request failed
options.error(result);
}
});
}
}
});
dataSource.fetch(function() {
console.log(dataSource.view().length);
// displays "77"
});
Use Function (Passing Local data)
var dataSource = new kendo.data.DataSource({
transport: {
read: function (e) {
// on success
e.success(sampleData);
// on failure
//e.error("XHR response", "status code", "error message");
},
create: function (e) {
// assign an ID to the new item
e.data.ProductID = sampleDataNextID++;
// save data item to the original datasource
sampleData.push(e.data);
// on success
e.success(e.data);
// on failure
//e.error("XHR response", "status code", "error message");
},
update: function (e) {
// locate item in original datasource and update it
sampleData[getIndexById(e.data.ProductID)] = e.data;
// on success
e.success();
// on failure
//e.error("XHR response", "status code", "error message");
},
destroy: function (e) {
// locate item in original datasource and remove it
sampleData.splice(getIndexById(e.data.ProductID), 1);
// on success
e.success();
// on failure
//e.error("XHR response", "status code", "error message");
}
},
// error handling
error: function (e) {
// handle data operation error
alert("Status: " + e.status + "; Error message: " + e.errorThrown);
},
pageSize: 10,
batch: false,
schema: {
model: {
id: "ProductID",
fields: {
ProductID: { editable: false, nullable: true },
ProductName: { validation: { required: true } },
Introduced: { type: "date" },
UnitPrice: { type: "number", validation: { required: true, min: 1} },
Discontinued: { type: "boolean" },
UnitsInStock: { type: "number", validation: { min: 0, required: true } }
}
}
}
});
Using local function
read: {
// same options as above in "create" and "destroy".
data: function() {
// this is how you specify data as a function.
return {
id: 42,
name: "John Doe"
};
}
ParameterMap
The function which convert the request parameters to a format suitable for the remote service. It is often to encode parameters in JSON format.
parameterMap: function(data, type) {
/*
data:
- The parameters which will be sent to the remote service.
data.models :
- all changed data items.
*/
return ...
}
Schema
The schema object within a DataSource is responsible for describing the raw data format. It is used to parse the remote server response.
It functions at a higher level than the model, and can even contain a model definition within it. The Schema's job is to instruct the DataSource on where to find information on errors, aggregates, data, groups, and total records within the raw data object that the dataSource is using. Schema has several important roles:
- Define the data field type in the shema.model.fields
- Define the id field of the data items in the schema.model.id. This ensures the correct adding, editing, and deleting of items.
- Define the key, which points to the data items array in schema.data.
schema: {
errors: function(response) {
return response.errors;
},
/*
The aggregates option is used only when the serverAggregates option is set to true.
*/
aggregates: function(response) {
return response.aggregates;
},
data: function(response) {
return response.data;
},
total: function(response) {
// total is returned in the "total" field of the response
return response.totalCount;
}
}
/*
In the preceding code sample, each of the properties has been set to a
function which, when passed the raw data object, will return the
appropriate data from within that object. These properties can also be
set to text filds, in which case the fild name given must exist at the
top level of the object and already contain the appropriate data in the
appropriate format:
*/
schema: {
errors: "errors_field_name",
aggregates: "aggregates_field_name",
data: "data_field_name",
total: "total_field_name"
}
aggregates
The field from the response which contains the aggregate results. Can be set to a function which is called to return the aggregate results from the response.
{
Field1Name: {
Function1Name: Function1Value,
Function2Name: Function2Value
},
Field2Name: {
Function1Name: Function1Value
}
}
The DataSource object has not calculated these values; rather they are already present in the raw data sent from the remote server, and the schema has indicated to the DataSource object where to locate them. It is possible to use a function to calculate aggregate values, but it is normal for the raw data to already contain these values within it as returned by a remote server.
The
aggregates
option is used only when the serverAggregates option is set totrue
.If set to
true
the data source will leave the aggregate calculation to the remote service. By default the data source calculates aggregates client-side.
var dataSource = new kendo.data.DataSource({
transport: {
/* transport configuration */
},
serverAggregates: true,
// calculated on the client side
aggregate: [
{ field: "age", aggregate: "sum" }
],
schema: {
aggregates: "aggregates"
// aggregate results are returned in the "aggregates" field of the response
}
});