【问题标题】:Nested JSON in dataview with Sencha Architect使用 Sencha Architect 在数据视图中嵌套 JSON
【发布时间】:2012-09-25 11:13:37
【问题描述】:

我想在我的 Sencha Touch 2 项目的数据视图中显示嵌套的 JSON 数据。 JSON 包含带有进度的服务请求的详细信息。

JSON:

{
    "success": true,
    "message": "Request retrieved with id:789",
    "data": {
        "id": 789,
        "description": "request description",
        "subject": "request subject",
        "progresses": [
            {
                "description": "progress description 1"
            },
            {
                "description": "progress description 2"
            },
            {
                "description": "progress description 3"
            }
        ]
    }
}

为了获取数据,我有两个模型,一个用于 requestdetails,一个用于进度,当然还有一个 store。

我的商店:

Ext.define('MyApp.store.RequestStore', {
    extend: 'Ext.data.Store',
    requires: [
        'MyApp.model.RequestModel'
    ],
    config: {
        autoLoad: true,
        autoSync: true,
        model: 'MyApp.model.RequestModel'
    }
});

还有我的模特:

Ext.define('MyApp.model.RequestModel', {
    extend: 'Ext.data.Model',
    uses: [
        'MyApp.model.ProgressModel'
    ],
    config: {
        fields: [
            {
                name: 'id'
            },
            {
                name: 'description'
            },
            {
                name: 'subject'
            }
        ],
        hasMany: {
            associationKey: 'progresses',
            model: 'MyApp.model.ProgressModel'
        },
        proxy: {
            type: 'rest',
            url: 'http://myurl.com/rest/request/789',
            reader: {
                type: 'json',
                rootProperty: 'data'
            }
        }
    }
});

Ext.define('MyApp.model.ProgressModel', {
    extend: 'Ext.data.Model',
    uses: [
        'MyApp.model.RequestModel'
    ],
    config: {
        fields: [
            {
                name: 'description'
            }
        ],
        belongsTo: {
            model: 'MyApp.model.RequestModel'
        }
    }
});

这是我的看法:

Ext.define('MyApp.view.MyDataView', {
extend: 'Ext.dataview.DataView',

config: {
    store: 'RequestStoreId',
    itemTpl: [
        '<div>',
        '   <div>ID</div>',
        '   <div>{id}</div>',
        '           ',
        '   <div>Subject</div>',
        '   <div>{subject}</div>',
        '           ',
        '   <div>Description</div>',
        '   <div>{description}</div>',
        '</div>',
        '',
        '<h2><b>Progress:</b></h2>',
        '<tpl for="progressmodels">',
        '    <div>',
        '        <div>{description}</div>',
        '    </div>',
        '</tpl>'
    ]
}
});

显示idrequestTypedescriptionsubject 工作正常。如果是进度,我希望显示所有进度描述,但现在只显示第一个描述。

我尝试了很多并搜索了互联网以解决它,但我无法弄清楚它是什么。

我在这里做错了什么?我错过了什么吗?有同样问题的人吗?

提前致谢!

【问题讨论】:

  • 您找到解决方案了吗?我也遇到了和你一样的问题
  • 是的,我有。 &lt;tpl for="progresses"&gt; 为我工作,而不是 &lt;tpl for="progressmodels"&gt;。唯一的问题是您无法转换模型中进度节点(例如日期)中的数据。您必须在 Xtemplate 本身中执行此操作。

标签: json sencha-touch-2 sencha-architect


【解决方案1】:

更换

&lt;tpl for="progressmodels"&gt;

&lt;tpl for="progresses"&gt;

为我工作。 progresses 节点是我要循环的节点。唯一的缺点是您无法使用模型转换节点中的数据。据我所知,您必须在您的 XTemplate 中执行此操作:)

希望这会对某人有所帮助!

【讨论】:

  • 默认情况下,根节点名称应该是目标模型名称的复数形式,但不一定。您还可以为您的阅读器指定“根”配置参数以覆盖它(在这种情况下坚持使用“进度”)。
猜你喜欢
  • 2012-10-06
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多