Events
You can subscribe to all DataSource events and then use them to further customize the behavior of the DataSource.
The example below demonstrates how to use the Error
, RequestStart
and RequestEnd
events.
@using Kendo.Mvc.UI
@(Html.Kendo().DataSource<Kendo.Mvc.Examples.Models.ProductViewModel>()
.Name("dataSource1")
.Ajax(dataSource => dataSource
.Read(read => read.Action("Products_Read", "DataSource"))
.ServerOperation(true)
.PageSize(12)
.Events(e=>e.Error("error_handler").RequestStart("onRequestStart").Request("onRequestEnd"))
)
)
<kendo-datasource name="dataSource1" type="DataSourceTagHelperType.Ajax" server-operation="true" page-size="12"
on-error="error_handler"
on-request-end="onRequestEnd"
on-request-start="onRequestStart">
<transport>
<read url="@Url.Action("Products_Read", "DataSource")" />
</transport>
</kendo-datasource>
function error_handler(e){
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
function onRequestStart(e){
if(e.type=="create"){
//apply logic
}
}
function onRequestEnd(e){
//access the raw remote service response
console.log(e.response);
}