javascript - 如何在 extjs 中为 json 数组创建模型?

标签 javascript json extjs

我从服务器取回的数据如下所示:

["admin", "user", "developer", "super"]

我正在尝试将其映射到此模型中:

Ext.define('RoleModel', {
      extend: 'Ext.data.Model',
      //fields: [{}]
});

我的商店是这样的:

 Ext.create('Ext.data.Store', {
      model: 'RoleModel',
      proxy: {
          type: 'ajax',
          url : '/roles',
          reader: {
              type: 'json'
          }
      },
      autoLoad: true
  });

如何将从服务器获取的单个数组映射到我的 RoleModel

最佳答案

您已经知道了,在您的 JSON 可以被解析为您的数据模型之前,它需要采用以下形式:

[
    { "role": "admin"     },
    { "role": "user"      }, 
    { "role": "developer" }, 
    { "role": "super"     }
]

...但是如果您使用的是 ExtJS 5.x,则无需手动进行 Ajax 调用 - 您可以应用 < em>transform 在代理的阅读器上,例如:

Ext.create('Ext.data.Store', {
    model: 'RoleModel', 
    proxy: {
        type: 'ajax',
        url : '/roles',
        reader: {
            type: 'json',
            transform: function(data) {
                return data.map(function(x){ 
                    return { role: x }; 
                });
            }
        }
    }
});

» Fiddle

关于javascript - 如何在 extjs 中为 json 数组创建模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810864/

相关文章:

javascript - 应用程序缓存错误 : Invalid or missing manifest origin trial token

c# - linq按子字符串对数组中的字符串进行排序

javascript - 在 pixi.js 中初始加载后访问纹理

javascript - 需要 Canvas 模糊工具

javascript - 如何在 HTML 中使用相同的输入值来拥有一个输入字段、两个按钮和两个操作

json - 为什么 Go json.Marshal 拒绝这些结构标签? json 标签的正确语法是什么?

javascript - 从以下 JSON 对象中将数据作为两个单独的数组获取的最有效方法是什么?

javascript - 无法在 extJS 窗口上设置高度

javascript - extjs:如何用树节点的直接子节点填充组合框?

javascript - 在检查元素中声明变量时未定义