【问题标题】:How to send the json data to server in a format from extjs Json Store如何以 extjs Json Store 的格式将 json 数据发送到服务器
【发布时间】:2013-01-06 14:34:34
【问题描述】:

我有一个应用程序,它会在页面加载时获得如下所示的 json 响应。

{
{"level2List":[{id:'id1', name:'sample'},....]},
{"level3List":[{id:'id1', name:'sample'},....]},
{"level4List":[{id:'id1', name:'sample'},....]}}

所以我正在获取根元素中的所有记录,例如“level2List”、“level3List”、level4List 等。通过使用这些记录,我正在创建一个可编辑的网格。当用户编辑任何记录时,我正在调用 store.sync 方法。然后它仅将已编辑记录的 json 发送到后端服务器。喜欢

[{id:'id1', name:'sample'},....]

但我需要以与页面加载相同的格式发送 json 数据。我的意思是我需要发送已编辑的记录及其所属的根元素。

任何帮助将不胜感激。提前谢谢..

【问题讨论】:

    标签: extjs extjs4.1 jsonstore


    【解决方案1】:

    假设您的模型实例知道它们属于哪些“根元素”,您可以通过创建自定义编写器来实现这一点。例如,如果您扩展 Ext.data.writer.Json,并在其中创建自己的 writeRecords() 方法,则可以动态定义要发送的根。

    writeRecords: function(request, data) {
        var root = this.root; // HERE IS WHERE YOU COULD DEFINE YOUR ROOT
        if (this.allowSingle && data.length == 1) {
            // convert to single object format
            data = data[0];
        }
        if (this.encode) {
            if (root) {
            // sending as a param, need to encode
                request.params[root] = Ext.encode(data);
            } else {
                //<debug>
                Ext.Error.raise('Must specify a root when using encode');
                //</debug>
            }
        } else {
            // send as jsonData
            request.jsonData = request.jsonData || {};
            if (root) {
                request.jsonData[root] = data;
            } else {
                request.jsonData = data;
            }
        }
        return request;
    }
    

    你只需要在你的代理上使用自定义编写器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      相关资源
      最近更新 更多