|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: Events |
| 4 | +description: "Learn how to handle the events of the Telerik UI DataSource component for {{ site.framework }}." |
| 5 | +slug: datasource_events |
| 6 | +position: 5 |
| 7 | +--- |
| 8 | + |
| 9 | +# Events |
| 10 | + |
| 11 | +You can subscribe to [all DataSource events](/api/kendo.mvc.ui.fluent/datasourceeventbuilder) and then use them to further customize the behavior of the DataSource. |
| 12 | + |
| 13 | +The example below demonstrates how to use the [`Error`](/api/kendo.mvc.ui.fluent/datasourceeventbuilder#errorsystemstring), [`RequestStart`](/api/kendo.mvc.ui.fluent/datasourceeventbuilder#requeststartsystemstring) and [`RequestEnd`](/api/kendo.mvc.ui.fluent/datasourceeventbuilder#requestendsystemstring) events. |
| 14 | + |
| 15 | +```HtmlHelper |
| 16 | + @using Kendo.Mvc.UI |
| 17 | +
|
| 18 | + @(Html.Kendo().DataSource<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 19 | + .Name("dataSource1") |
| 20 | + .Ajax(dataSource => dataSource |
| 21 | + .Read(read => read.Action("Products_Read", "DataSource")) |
| 22 | + .ServerOperation(true) |
| 23 | + .PageSize(12) |
| 24 | + .Events(e=>e.Error("error_handler").RequestStart("onRequestStart").Request("onRequestEnd")) |
| 25 | + ) |
| 26 | + ) |
| 27 | +``` |
| 28 | +{% if site.core %} |
| 29 | +```TagHelper |
| 30 | + <kendo-datasource name="dataSource1" type="DataSourceTagHelperType.Ajax" server-operation="true" page-size="12" |
| 31 | + on-error="error_handler" |
| 32 | + on-request-end="onRequestEnd" |
| 33 | + on-request-start="onRequestStart"> |
| 34 | + <transport> |
| 35 | + <read url="@Url.Action("Products_Read", "DataSource")" /> |
| 36 | + </transport> |
| 37 | + </kendo-datasource> |
| 38 | +``` |
| 39 | +{% endif %} |
| 40 | +```JavaScript |
| 41 | + function error_handler(e){ |
| 42 | + if (e.errors) { |
| 43 | + var message = "Errors:\n"; |
| 44 | + $.each(e.errors, function (key, value) { |
| 45 | + if ('errors' in value) { |
| 46 | + $.each(value.errors, function () { |
| 47 | + message += this + "\n"; |
| 48 | + }); |
| 49 | + } |
| 50 | + }); |
| 51 | + alert(message); |
| 52 | + } |
| 53 | + } |
| 54 | + function onRequestStart(e){ |
| 55 | + if(e.type=="create"){ |
| 56 | + //apply logic |
| 57 | + } |
| 58 | + } |
| 59 | + function onRequestEnd(e){ |
| 60 | + //access the raw remote service response |
| 61 | + console.log(e.response); |
| 62 | + } |
| 63 | +``` |
| 64 | + |
| 65 | +## Next Steps |
| 66 | + |
| 67 | +* [API for Configuring the DataSource Events](/api/kendo.mvc.ui.fluent/datasourceeventbuilder) |
| 68 | +* [Using the DataSource Events (Demo)](https://demos.telerik.com/{{ site.platform }}/datasource/events) |
| 69 | + |
| 70 | +## See Also |
| 71 | + |
| 72 | +* [Using the API of the DataSource for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/datasource/api) |
0 commit comments