【问题标题】:Sencha ExtJS Json TransformationSencha ExtJS Json 转换
【发布时间】:2013-03-14 13:55:18
【问题描述】:

所以,我正在制作一个登录表单。

Sencha 期望任何 Json 响应通常具有以下格式:

{  
  "root" : {
    "success": true/false,
    "message": ...,
    "id": ...,
    "metaData": ...,
  }
}

但是,我得到的实际 Json 响应如下所示:

{
  "SessionId":"1", //doesn't exist if login error
  "UserName":"admin", //doesn't exist if login error
  "ResponseStatus":{
    //blank or failed to login error list 
  }
}

格式来自第 3 方,所以我无法更改。由于结构不同,无法直接映射这些文件。

我的直觉告诉我应该在 Ext.Form -> Reader/errorReader -> Functions 中编写自定义 json 数据转换。然而,我是 ExtJS 的新手,我没有脑力将它粘合在一起。你能帮忙吗?

【问题讨论】:

    标签: javascript json extjs


    【解决方案1】:

    按照 Johan 的说法,您不需要自定义阅读器。

    如果你的 json 没有根,那么不要告诉你的读者使用根...

    Ext.define('My.model.Model1', {
        extend:'Ext.data.Model',
        fields:[
        'SessionId',
        'UserName'
        ],
        proxy:{
          type:'ajax',
          url:'./app/data/response.json',
          reader:{
            type:'json',
            messageProperty:'ResponseStatus'
          }
        }
    });
    

    【讨论】:

    • 接近了,但你看,我使用的是 Ext.Form.Panel 和表单中的阅读器。所以没有模型或代理?我该如何适应?
    【解决方案2】:

    这可以做到,您需要创建自己的阅读器。这篇文章中有这样的答案:

    how to configure extjs reader to read this json?

    Ext.define('Ext.data.reader.JsonPWithoutRoot', {
        extend: 'Ext.data.reader.Json',
        read: function(response) {
            return this.callParent([ { root: response } ]);
        },
        root: 'root'
    });
    

    您的商店代理配置:

    proxy: {
        type    : 'json',
        reader  : {
            type: 'json',
            root: ''
        }       
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      相关资源
      最近更新 更多