如何在kendo.data.Datasource上获得单个结果

时间:2013-10-24 15:49:10

标签: kendo-ui kendo-mobile kendo-mvvm

我有像这样的Odata结果

        {"odata.metadata":"https://localhost/DocTalkMobileWebApiOData/odata/$metadata#MasterPatient/@Element","PatUniqueId":"39e713db-6a0e-4e59-bf7b-033f4fc47ad5",  "PatID":null,
"pat_lname":"White","pat_fname":"Peter","pat_mi":"     ","pat_ssn":"270787655","pat_dob":"08/07/1973","pat_sex":"M","pat_status":null,"priInsID":2,"secInsID":1,"PCPID":1,"InternalDrID":1,"EXPID":1,"EXPDate":"","pat_phone":null,"isNew":true,"imported":true,"byWhom":"dt","lastUpdate":"2011-03-30T09:41:57.36","changeStamp":"AAAAAAAAIUE=","address":"","city":"","state":"","zip":"","currentMcp":"","currentVisitCount":-2,"otherId":"543674","pcpName":null,"hasChanges":true,"ProgramSource":null,"mrnID":"","createdBy":null,"createdDate":"2007-10-26T10:16:15","expLocation":null,"ethnicId":1,"prefLanguageId":1,"raceId":1
    }

我尝试通过kendo.ui.datasource:

获得此结果
 newPatient = new kendo.data.DataSource({
        type: 'odata', // <-- Include OData style params on query string.
        transport: {
            read: {
                url: url + '/MasterPatient(guid\'00000000-0000-0000-0000-000000000000\')', // <-- Get data from here
                dataType: "json" // <-- The default was "jsonp"
            },

            parameterMap: function (options, type) {
                var paramMap = kendo.data.transports.odata.parameterMap(options);

                delete paramMap.$inlinecount; // <-- remove inlinecount parameter.
                delete paramMap.$format; // <-- remove format parameter.

                return paramMap;
            }
        },
        schema: {
            data: function (data) {                 
                return data;
            },
            total: function (data) {                 
             return  data['odata.count']
            },
        }
    });
    newPatient.fetch(function () {
        kendo.bind($('#newPatientTab'), newPatient);
    });

但不确定为什么总会抛出错误:

Uncaught TypeError: Object [object global] has no method 'slice' 

请帮帮我。感谢

2 个答案:

答案 0 :(得分:6)

在Kendo UI中,DataSource仅适用于数组。如果您可以更改服务器响应以发送类似这样的内容

[{"odata.metadata":"https://localhost/DocTalkMobileWebApiOData/odata/$metadata#MasterPatient/@Element","PatUniqueId":"39e713db-6a0e-4e59-bf7b-033f4fc47ad5","PatID":null,"pat_lname":"White","pat_fname":"Peter","pat_mi":"     ","pat_ssn":"270787655","pat_dob":"08/07/1973","pat_sex":"M","pat_status":null,"priInsID":2,"secInsID":1,"PCPID":1,"InternalDrID":1,"EXPID":1,"EXPDate":"","pat_phone":null,"isNew":true,"imported":true,"byWhom":"dt","lastUpdate":"2011-03-30T09:41:57.36","changeStamp":"AAAAAAAAIUE=","address":"","city":"","state":"","zip":"","currentMcp":"","currentVisitCount":-2,"otherId":"543674","pcpName":null,"hasChanges":true,"ProgramSource":null,"mrnID":"","createdBy":null,"createdDate":"2007-10-26T10:16:15","expLocation":null,"ethnicId":1,"prefLanguageId":1,"raceId":1}]

然后它会正常工作。 注:它是数组格式。

OR

您可以将单个对象包装到客户端的数组中,在模式的数据函数内。

schema: {
  data: function(server-response) {
    return [server-response];
  }
}

剑道团队应该把更多时间放在好文档上。

答案 1 :(得分:3)

这意味着您没有使用来自支持的odata源。你需要考虑一下,如果你的后端不支持odata,你真的需要来自客户端的kendo odata源。

请参阅odata url的回复,http://services.odata.org/Northwind/Northwind.svc/?$ format = json

它应该在value字段中返回一个对象数组。 如果您无法更改支持,则可以在Schema.data函数中格式化数据

schema: {
        data: function (data) {                 
            return [data];
        },