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

Different root elements in JSON returned by different CRUD methods

3 Answers 206 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 10 May 2012, 09:48 PM
I have a problem with a dataSource for a kendo grid. The dataSource consumes JSON endpoints for CRUD operations on a remote DB.

The JSON endpoints that the dataSource is consuming contain different root elements for read versus the other methods. The root element for a read is "GetFooResult.RootResults". The root element for all other methods is "SubmitChangesResult". The endpoints are created by the .NET DomainServices JSON endpoint factory, so I don't necessarily have influence over what format they return.

The problem is that the dataSource is not able to parse the returned JSON for methods except read, since that is what the dataSource:schema:data element is based on. Therefore when I execute a create operation it does not recognize the returned JSON; each subsequent create continues to attempt to send all previous create attempts, which creates duplicate records in the DB.

Any idea how to specify different dataSource:schema:data elements for the various transport methods? There seems to be only one schema with one data element per datasource. Could I write code under parameterMap that sets this element depending on the transport method?

Thanks,
David

3 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 11 May 2012, 07:50 AM
Hello David,

The data property of the schema could be a function:
datasource = new kendo.data.DataSource({
    transport: {
        //....
    },
    schema: {
        data: function(data) {
            //....
            return dataArray;
        }           
    }
})

In this way you could find and return the array containing your data.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
David
Top achievements
Rank 1
answered on 11 May 2012, 04:03 PM
Sounds good, but I'm a js newb so I need a little more help ;-)
Here's what I've done:

data: function (data) {
var dataArray = [];
try {
dataArray = data.GetFooResult.RootResults;
}
catch (err) {
dataArray = data.SubmitChangesResult;
}
return dataArray;
},


This seems to be getting around the issue, but the script is still sending create calls over and over.
0
David
Top achievements
Rank 1
answered on 11 May 2012, 05:21 PM
Alexander FTW!!

data: function (data) {
    var dataArray = [];
    try {
        dataArray = data.FooResult.RootResults;
    }
    catch (err) {
        dataArray[0] = data.SubmitChangesResult[0].Entity;
    }
    return dataArray;
},


I needed to go a bit deeper into the format submitted by the create method.

THANK YOU!
Tags
Data Source
Asked by
David
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
David
Top achievements
Rank 1
Share this question
or