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

Showing all data

5 Answers 37 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Norm
Top achievements
Rank 1
Norm asked on 24 Apr 2013, 10:14 PM
I am using DataSource to consume data from a remote resource, like this:

DataSource.Products = new kendo.data.DataSource({
  transport: {
    read: {
      url: "https://wwww.",
      dataType: "json",
      type: "GET"
    }
  },
});

My response should be JSON like this:
{
    "status": {
        "http_code": 200
    },
    "contents": {
        "totalSize": 5,
        "done": true,
        "records": [
            {
                "attributes": {
                    "type": "",
                    "url": ""
                },
                "Name": ""
            },......
]

At minimum I would just like to print the response:
DataSource.Products.fetch(function(){
  var data = this.data();
  console.log(data);
});

But the output is not what I expect, in fact I can't see the underlying data.

Let's say for example I want to see the records array, can I do data.contents.records?

5 Answers, 1 is accepted

Sort by
0
Norm
Top achievements
Rank 1
answered on 24 Apr 2013, 10:25 PM
Ahh nevermind, I found that  schema: {
data: function(response) {
return response.contents.records; 
}
}

would allow me to see the records by just doing data[0].get('Name')
0
Norm
Top achievements
Rank 1
answered on 25 Apr 2013, 03:28 PM
Arrggg ... Does DataSource only support data in a 1-level array structure?

In my previous post I said that I can get by with:
schema: {
  data: function(response) {
    return response.contents.records
  }
}

But what if I want to use response.foo as well?
0
Atanas Korchev
Telerik team
answered on 26 Apr 2013, 01:41 PM
Hello Norm,

Yes, the DataSource can bind to only one array of data. If you want to use two fields from your response just merge them in the data method of your schema definition:

schema: {
   data: function(response) {
       var result = [];
       
       for (var i = 0; i < response.foo.length; i++) {
           result.push(response.foo[i]);
       }
     
      for (var j = 0; j < response.bar.length;j++) {
           result.push(response.bar[i]);
       }
 
       return result;
   }
}

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Norm
Top achievements
Rank 1
answered on 26 Apr 2013, 04:12 PM
Thank you ...

I found that if I use the schema: { data: function (response){....} approach then it is never executed if I trigger my datasource with just a read();
0
Atanas Korchev
Telerik team
answered on 29 Apr 2013, 06:58 AM
Hello,

 Here is a demo showing that schema.data is executed when the read method is called: http://jsbin.com/azoyuy/1/edit

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Norm
Top achievements
Rank 1
Answers by
Norm
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or