How can I read the expand Entity using Kendo UI Datasource Schema?

May 8, 2014 at 4:13 PM
Edited May 8, 2014 at 4:24 PM
I am trying to read the Entity1 using the dataSource schema from the returned json object.


The Json Object is as follow
{
  "odata.metadata":"http://localhost:25061/odata/$metadata#Entity,"value":[
    {
      "Entity1":{
        "LastName":"Smith","FirstName":"James"},"Id":1,"Other":2},{
      "Entity1":{
        "LastName":"Smith","FirstName":"Alex"},"Id":2,"Other":3}
    }]
}
Datasource Schema
  schema: {
                data: function (data) { return data.value; },
                total: function (data) { return data["odata.count"]; },
            },
Model
 $("#grid").kendoGrid({
            dataSource: dataSource,
            filterable: true,
            sortable: true,
            pageable: true,
            columns: [
                 { field: "LastName" },
                { field: "FirstName" }
            ]
        });
Thanks,
DG
May 12, 2014 at 5:20 AM
Edited May 12, 2014 at 5:22 AM
This is exampled in customerController.js and customerDataSource.js.

Return data.value which will be your array of type Entity1 and access it by index of the element of your array.

customerEditController.js

https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Sample/Northwind.Web/Scripts/app/controllers/customerEditController.js
'use strict';

northwindApp.controller('customerEditController',
    function ($scope, $routeParams, $location, customerDataSource)
    {
        var customerId = $routeParams.id;

        customerDataSource.filter({ field: "CustomerID", operator: "eq", value: customerId });
        $scope.customer = customerDataSource.at(0);

        $scope.save = function ()
        {
            customerDataSource.view()[0].dirty = true;
            customerDataSource.sync();
            $location.url('/customer');
        };

        $scope.cancel = function ()
        {
            $location.url('/customer');
        }
    });
customerDataSource.js

https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Sample/Northwind.Web/Scripts/app/services/customerDataSource.js
'use strict';

northwindApp.factory('customerDataSource',
    function (customerModel)
    {
        var crudServiceBaseUrl = "/odata/Customer";

        return new kendo.data.DataSource({
            type: "odata",
            transport: {
                read: {
                    async: false,
                    url: crudServiceBaseUrl,
                    dataType: "json"
                },
                update: {
                    url: function (data)
                    {
                        return crudServiceBaseUrl + "(" + data.CustomerID + ")";
                    },
                    type: "put",
                    dataType: "json"
                },
                destroy: {
                    url: function (data)
                    {
                        return crudServiceBaseUrl + "(" + data.CustomerID + ")";
                    },
                    dataType: "json"
                }
            },
            batch: false,
            serverPaging: true,
            serverSorting: true,
            serverFiltering: true,
            pageSize: 10,
            schema: {
                data: function (data) { return data.value; },
                total: function (data) { return data["odata.count"]; },
                model: customerModel
            },
            error: function (e)
            {
                alert(e.xhr.responseText);
            }
        });
    });
Marked as answer by lelong37 on 5/11/2014 at 9:22 PM