1

我有一个BloghasMany Posts(和许多其他领域)的模型。现在我想在这样的List视图中列出这些帖子:

  • [我的帖子#1]
  • [我的帖子#2]
  • [我的帖子#3]

就所描述的 API 而言,我可以将存储或数据属性传递给 Ext.dataview.List。但是我不知道如何将 hasMany 记录传递到列表中,因此它将为每个记录显示一个项目。

我真的必须创建另一家商店吗?是否可以将我的数据视图配置为类似store: 'Blog.posts'data: 'Blog.posts'什至records: 'Blog.posts'

4

3 回答 3

0

扩展 dataview.List 以定义 itemtpl 以循环浏览帖子

itemTpl: new Ext.XTemplate(
    '<tpl for="**posts**" >',
        '<div>{TheBlogPost}</div>',
            '</tpl>'
)
于 2012-12-10T04:43:24.743 回答
0

如果您知道如何访问它们,Sencha 会从关联中自动生成存储。因此,您只需在加载时将列表的存储切换为自动生成的“子存储”。

这种方法可能存在一些问题,例如在使用列表分页插件时,但它很快。

例子:

楷模

Ext.define('Conversation', {
extend: 'Ext.data.Model',
config: {
    fields: [

    ],
    associations:
        [
            {
                type: 'hasMany',
                model: "Message",
                name: "messages",
                associationKey: 'messages'
            }
        ]
}
});

Ext.define('Message' ,
{
    extend: "Ext.data.Model",
    config: {
        idProperty: 'id_message',
        fields: [
            { name: 'text', type: 'string' },
            { name: 'date', type: 'string' },
            { name: 'id_message', type: 'int' },
            { name: 'me', type: 'int'} // actually boolean
        ]
    }
}
    );

JSON

 [
{
    "messages": [

    {"id_message": 11761, "date": 1378033041, "me": 1, "text": "iiii"},
    {"id_message": 11762, "date": 1378044866, "me": 1, "text": "hallo"}
]}
]

控制器

        this.getList().getStore().load(
            {
                callback: function(records, operation, success) {

//IMPORTANT LINE HERE:
getList().setStore(Ext.getStore(me.getList().baseStore).getAt(0).messages());

                },
                scope: this
            }

        );

列表显示

                            {
                                flex: 1,
                                xtype: 'list',
                                itemId: 'ConversationList',
                                data: [],
                                store: 'ConversationStore',
                                baseStore: 'ConversationStore',
                                itemTpl:
                                        '                {[app.util.Helpers.DateFromTimestamp(values.date)]}<br><b>{name}</b>' +
                                        '           {[app.util.Helpers.fixResidualHtml(values.text)]} </div>' +
                            },
于 2013-10-04T19:25:02.047 回答
0

正如@Adam Marshall 所说,这并不像我想象的那么容易。

于 2012-12-16T14:19:28.643 回答