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

How to use kendo datasource when a REST service returns only one element

2 Answers 376 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
pedro
Top achievements
Rank 1
pedro asked on 07 Feb 2013, 12:56 PM
I have a service (ASP.NET WebApi) whose URL returns a single object:

http://localhost:60000/api/approvals/90a11073-cbfe-4442-9330-9b94d83d53a2

But I can't get the datasource to handle the scenario. The request is made and the response is ok but in the change event I don't have any data. the ok from qunit fails.

datasource.bind("change", function (e)
    {
        var element = datasource.get('90a11073-cbfe-4442-9330-9b94d83d53a2')
        ok(element);
        start();
    });

Also inspecting e.sender.data() shows that there are no elements in the result!

Is this supported? How?

Thanks,
Pedro

2 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 07 Feb 2013, 03:07 PM
Hi Pedro,

 The Kendo DataSource works with arrays. If your service returns a single element you need to convert it to array with one item. This is done by specifying the schema.data setting as a function:

schema: {
    data: function(response) {
     return [response]; // create an array from the response
    }
}


I hope this helps,
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
pedro
Top achievements
Rank 1
answered on 07 Feb 2013, 04:42 PM
For reference here goes my solution:

// set the id of the model to the property named "id"
self.DefaultItem = kendo.data.Model.define({
        id: "id"
    });
 
self.dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: self.serviceAddress
            }
        },
        schema: {
            model: self.DefaultItem,
            data: function (result) {
                if ($.isArray(result)) {
                    return result;
                }
                else {
                    return [result];
                }
            }
        },
// ... remaining datasource options

Rgds,
Pedro
Tags
Data Source
Asked by
pedro
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
pedro
Top achievements
Rank 1
Share this question
or