This is a migrated thread and some comments may be shown as answers.

Extend Kendo.Mvc.UI.DataSourceResult with new property

2 Answers 829 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Ruairi
Top achievements
Rank 1
Ruairi asked on 19 Oct 2016, 11:24 AM

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 Answers, 1 is accepted

Sort by
0
Ruairi
Top achievements
Rank 1
answered on 20 Oct 2016, 07:58 AM

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);
  }
}

 

0
Alex Hajigeorgieva
Telerik team
answered on 21 Oct 2016, 07:33 AM
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.
Tags
Data Source
Asked by
Ruairi
Top achievements
Rank 1
Answers by
Ruairi
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Share this question
or