javascript - Kendo DataSource 在数据源同步上附加 Schema Id

标签 javascript arrays asp.net-web-api kendo-ui

只是想知道为什么javascript的push方法会插入“index”

   var agendaBatch=[];

    for(var i=0; i<agendas.length; i++) {

        var agenda = {
            MeetingId: meetingId,
            Title: agendas[i].title,
            Description: agendas[i].description,
            Remarks: "",
        };

        agendaBatch.push(agenda);   
    }

    console.log(kendo.stringify(agendaBatch));
    dataSourceAgenda.add(agendaBatch);

    dataSourceAgenda.sync();

输出:

{"0":{"Title":"Agenda title","Description":"Agenda details","Remarks":""},
 "1":{"Title":"Agenda title","Description":"Agenda details","Remarks":""}}

我期望此输出符合我的 Web API 参数要求

[{"Title":"Agenda title","Description":"Agenda details","Remarks":""},
 {"Title":"Agenda title","Description":"Agenda details","Remarks":""}]

有什么建议我该怎么做?....

更新:刚刚发现,我正在使用 kendo ui 数据源,当我删除架构上的 Id 时我解决了这个问题

var dataSourceAgenda = new kendo.data.DataSource({
            transport: {
                type: "odata",
                create: {
                    type: "POST",
                    url: API_URL + "/agendas",
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json'
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options) {
                        return kendo.stringify(options);
                    } 
                }
            },
            schema: {
                model: {
                    id: "Id", //I get my desired output if this is removed
                    fields: {
                        MeetingId: { type: "number" },
                        Title: { type: "string" },
                        Description: { type: "string" },
                        Remarks: { type: "string" },
                    }
                },
            }        
        });  

但是我需要在其他函数中使用 Id 参数,无论如何我可以在不删除 kendo 数据源中的 Id 的情况下执行此操作。

更改了问题标题!

最佳答案

根据 Kendo UI DataSource ( here ) 的文档,add 方法接受一个 Object 而不是 Object 的 array

此外,您将一个名为 Id 的字段用作 id,该字段不在您的 model 字段中。

尝试执行以下操作:

var dataSourceAgenda = new kendo.data.DataSource({
    transport: {
        create      : function (op) {
            ...
        },
        parameterMap: function (options, operation) {
            if (operation !== "read" && options) {
                return kendo.stringify(options.models);
            }
        }
    },
    batch    : true,
    schema   : {
        model: {
            id    : "Id", //I get my desired output if this is removed
            fields: {
                Id         : { type: "number" },
                MeetingId  : { type: "number" },
                Title      : { type: "string" },
                Description: { type: "string" },
                Remarks    : { type: "string" }
            }
        }
    }
});

即:

  1. batch 设置为 true,以便在您调用 sync 时能够一次发送多个请求。
  2. schema.model.fields 定义中定义 Id
  3. 执行 options.modelsstringify

关于javascript - Kendo DataSource 在数据源同步上附加 Schema Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18352277/

相关文章:

javascript - 在表中搜索相同的日期并将复选框添加到 td

javascript - 如何将 ruby​​ 放入 javascript 函数、谷歌地图中?

java - 如何使方法将任何类型的数组作为参数?

java - RestTemplate 获取对象列表 - 为什么使用 ParameterizedTypeReference 而不是对象数组?

c# - 如何从我的 Web API 响应中删除 header ?

asp.net-web-api - Net Web API 发布到 Azure API 管理服务后出现 500 错误

javascript - 拉斐尔 JS 问题

javascript - 使用函数触发 chrome.browserAction.onClicked

javascript - 将 MongoDB Stitch 查询中的 objectID 或 _id 转换为字符串并映射它

mysql - linq 中的 Intersect 或 Except 方法在 MYSQL 中不起作用