【问题标题】:ExtJS 4.1 - Nested hasOne associated in JSONExtJS 4.1 - JSON 中关联的嵌套 hasOne
【发布时间】:2012-07-20 19:35:16
【问题描述】:

我有以下格式的 JSON:

{
    "id": 1,
    "arbitraryAttribute": "arbitraryValue",
    "thing": {
        "thingKey1": "thingValue1",
        "thinkKey2": "thingValue2"
    }
}

这个模型是这样表示的:

Ext.define("MyModel", {
    extends: "Ext.data.Model",
    fields: [
        {name: "id", type: "string"},
        {name: "arbitraryAttribute", type: "string"},
    ],
    hasOne: [
        {
            model: "Thing",
            name: "thing"
        }
    ]
});

Ext.define("Thing", {
    extends: "Ext.data.Model",
    fields: [
        {name: "thingKey1", type: "string"},
        {name: "thingKey2", type: "string"}
    ]
});

代理是一个简单的 json 代理。它得到的 JSON 看起来像我展示的那样,但我的记录似乎不知道 Thing 模型。是否需要设置任何额外的管道才能让 MyModel 提取嵌套的 Thing json?

【问题讨论】:

    标签: extjs extjs4.1


    【解决方案1】:

    您忘记在 MyModel 中设置 thing_id。这在 ExtJs 中也根本不起作用。 您现在可以通过 JSON 设置 thing_id,但不能像您所做的那样设置整个对象(而且应该如此)。

    如果需要,它将通过模型代理自动加载完整的对象。

    【讨论】:

      【解决方案2】:
      Ext.define('User', {
          extend:'Ext.data.Model',
          fields: ['id', 'name', 'status'],
          associations: [
              { type: 'hasOne', model: 'Status', associationKey: 'status'  }
          ]
      });
      
      Ext.define('Status', {
          extend:'Ext.data.Model',
          fields: ['id', 'title'],
      });
      

      Demo here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 2020-11-26
        • 1970-01-01
        • 2013-03-18
        • 1970-01-01
        相关资源
        最近更新 更多