Extend Kendo.Mvc.UI.DataSourceResult with new property

3 posts, 0 answers
  1. Tyler
    Tyler avatar
    46 posts
    Member since:
    Nov 2013

    Posted 19 Oct Link to this post

    I want to add an additional UserMessage property to the Kendo.Mvc.UI.DataSourceResult that I can access in the datasource change event on the client. The problem is I cant seem to access or see my new property in the javascript datasource Change event.

    I extend the existing Kendo.Mvc.UI.DataSourceResult class as below.

    public class KendoDataSourceResult : Kendo.Mvc.UI.DataSourceResult
     {
       public KendoDataSourceResult(Kendo.Mvc.UI.DataSourceResult dataSourceResult)
       {
         this.AggregateResults = dataSourceResult.AggregateResults;
         this.Data = dataSourceResult.Data;
         this.Errors = dataSourceResult.Errors;
         this.Total = dataSourceResult.Total;
       }
       //This is my new Property - I want to be able to access this on the
       public string UserMessage { get; set; }
     }

     

    In my datasource read method I have the following code which sets my new property.

    public ActionResult SearchResults_ReadData([DataSourceRequest]DataSourceRequest request)
    {
      IEnumerable<MyData> result = GetMyData();
     
      DataSourceResult x = result.ToDataSourceResult(request);
      KendoDataSourceResult returnResult = new KendoDataSourceResult(x);
       
      //Set my new property here
      returnResult.UserMessage = "My Test User Message<br/>Second line here<br/>Third line here";
      return Json(returnResult, JsonRequestBehavior.AllowGet);
    }

     

    How do I get the UserMessage property in my javascript Change event?

    function datasourceOnChange(e) {
      if (e && e.sender) {
        var dataSource = e.sender;
        
       //How do I get my UserMessage property ????
        var msg = dataSource.UserMessage; //this dosnt work
      }
    }

     

     

     

  2. Tyler
    Tyler avatar
    46 posts
    Member since:
    Nov 2013

    Posted 20 Oct in reply to Tyler Link to this post

    I have a solution that works.

    Rather than use the Change event I use the RequestEnd event with the javascrip below.

    I'm not sure why I cant access this property in the Change event.

     

    function onRequestEnd(e) {
      var msg = e.response.UserMessage;
      if ($("#dacMsg")) {
        $("#dacMsg").text(msg);
      }
    }

     

  3. Kendo UI is VS 2017 Ready
  4. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    115 posts

    Posted 21 Oct Link to this post

    Hi Tyler,

    I am glad that you have already found a solution how to access the custom property which is sent from the server to the Kendo UI Data Source.

    The property is part of the response object and this is why you are able to access it when the request ends. However, when the response is processed, a new instance of the Kendo UI Data Source is created (this is not the same object as the one that was received). The Kendo UI Data Source processes the response object(the one that holds the custom property), assigns the fields and options according to its configuration options and ignores everything else.

    To summarise - the Kendo UI Data Source and the response from the server are different objects. Since the custom property is not part of the data source configuration, it is skipped and left for the browser garbage collector to pick up at some point.

    I hope this helps.

    Regards,
    Alex Hajigeorgieva
    Telerik by Progress
     
    Build rich, delightful, *native* Angular 2 apps with Kendo UI for Angular 2. Try it out today! Kendo UI for Angular 2 (currently in beta) is a jQuery-free toolset, written in TypeScript, designed from the ground up to offer true, native Angular 2 components.
Back to Top