【问题标题】:Kendo DataSource appends Schema Id on datasoure syncKendo DataSource 在数据源同步上附加 Schema Id
【发布时间】:2013-08-23 12:30:21
【问题描述】:

只想知道为什么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.stringify 没有正确序列化数组的问题。 JSON.stringify 的结果是什么?

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


【解决方案1】:

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

此外,您将一个名为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. 执行stringifyoptions.models

【讨论】:

    【解决方案2】:

    由于agendaBatch 显然是一个数组,我假设kendo.stringify 没有正确序列化它。你可以选择JSON.stringify

    请注意,旧版浏览器并未实现此功能。如果你需要支持他们,你可以包含 Douglas Crockford 的脚本:

    https://github.com/douglascrockford/JSON-js/blob/master/json2.js


    编辑

    既然你改变了你的问题 - 我对剑道 ui 不是很熟悉,所以 这真的只是一个疯狂的猜测,试图帮助你解决更新后的问题。

    您似乎可以访问beforeSend 函数中的data。您可以尝试根据需要对其进行操作,例如:

    beforeSend: function (xhr, s) {
        var arrayData = [];
    
        for (var id in s.data) {
            arrayData.push(s.data[id]);
        }
    
        s.data = arrayData;
    }
    

    【讨论】:

    • 请看我的编辑,它实际上不是 kendo.stringify。它是在 fiddler 中传递的实际值。
    • 现在我对剑道 ui 不熟悉,但我做了一个编辑,至少可以帮助您找到解决方案...
    • 不工作.. 我想我必须手动删除 json 字符串中的索引
    • 当我对 s.data 进行字符串化时,这就是我得到的 ["{","\"","0","\"",":","{","\" ","T","i","t","l","e","\"",":","\"","A","g","e","n ","d","a"," ","t","i","t","l","e","\"",",","\"","D" ,"e","s","c","r","i","p","t","i","o","n","\"",":", "\"","A","g","e","n","d","a"," ","d","e","t","a","i ","l","s","\"",",","\"","R","e","m","a","r","k","s ","\"",":","\"","\"","}",",","\"","1","\"",":","{" ,"\"","T","i","t","l","e","\"",":","\"","A","g","e ","n","d","a",","t","i","t","l","e","\"",",","\"" ,"D","e","s","c"...
    • 我不知道它是怎么搞砸的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多