Extjs 通过ID连接两个模型

Extjs 通过ID连接两个模型,extjs,Extjs,几天前我问了一个ExtJS问题,作为补充,我还问了如何连接我的两个模型。主要答案得到了回答,但我仍然无法找出我的另一个问题,所以我为它打开了一个新的问题 这可能又是一个愚蠢的问题,但问题是: 我从服务器获得一个JSON,如下所示: { "success": true, "result": { "publishers": [ { "id": "009999", "type": "ABC", "isReceipien

几天前我问了一个ExtJS问题,作为补充,我还问了如何连接我的两个模型。主要答案得到了回答,但我仍然无法找出我的另一个问题,所以我为它打开了一个新的问题

这可能又是一个愚蠢的问题,但问题是:

我从服务器获得一个JSON,如下所示:

{
"success": true,
"result": {
    "publishers": [
        {
          "id": "009999",
          "type": "ABC",
          "isReceipient": false,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": true,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": false,
          "description": ""
        }
        ],
        "notes": [
        {
          "publisherId": "009999",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": "46",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": 45,
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        }
    ]
}
}
Ext.define('MA.view.sections.notes.NotesPanel' ,{
    extend: 'Ext.DataView',
    alias: 'widget.notes-panel',
    // deferInitialRefresh: true,

    itemSelector: 'div.notes-list',
    tpl:  new Ext.XTemplate(
        '<div class="notes-list">',
            '<tpl for=".">',
                '<p>{created}, by {publisherId}</p>',
                '<p>{text}</p>',
                '<hr />',
            '</tpl>',
        '</div>'
    ),

    emptyText: 'No data available',
    initComponent: function() {
        var me = this,
            publisherStore = Ext.data.StoreManager.lookup('Publishers');
        //me.addEvents(   //just messing here, trying stuff
        //    'user-offer-activities'
        //);
        me.callParent(arguments);
    }
    //renderTo: Ext.getBody()

})
;
所以我得到了两个数组,publisher和notes。我有两个模型,我使用loadRawData()将它们加载到控制器的模型中,它可以工作,我的所有发布者和注释都在商店中。(他们都有一个商店——出版商和笔记)。但是我需要在notes中使用publisherId来显示发布者描述。我尝试了使用google和sancha docs可以找到的很多东西:associations、hasmany、hasone、belongsto,以及创建由两个聚合模型组成的第三个存储。到目前为止,一切都没起作用

我想要的是有一个拥有所有笔记的商店,再加上所有笔记都有出版商信息

我将复制我下面的两个模型,你可以在那里看到,评论出我一直在尝试的东西。我也试着改变身份证,名字等,所以这些变化。但我永远也无法从笔记中得到出版商的信息

Ext.define('MA.model.Note', {
    extend: 'Ext.data.Model',
    fields: [
        'publisherId',
        'text' ,
        //hasone publisher
        {
            name: 'created',
            type: 'date',
            dateFormat: 'c'//'d-M-Y H:i:s' //"2014-02-23T18:24:06.074Z" needed: "02/23 18:24"
        }
    ]
    // hasOne: [
    //     {
    //         name: 'publisher',
    //         model: 'Publisher',
    //         associationKey: 'publisherId'
    //     }
    // ],

    // associations: [
    //     {
    //         type: 'hasOne',
    //         model: 'Publisher',
    //         primaryKey: 'id',
    //         foreignKey: 'publisherId'
    //     }
    // ]

    // associations : [
    //     {
    //         type           : 'hasOne',
    //         model          : 'MA.model.Publisher',
    //         getterName     : 'getPublisher',
    //         associatedName : 'User',
    //         associationKey : 'User'
    //     },
    //     {
    //         type           : 'belongsTo',
    //         model          : 'MA.model.Publisher',
    //         getterName     : 'getPublisher',
    //         associatedName : 'Publisher',
    //         associationKey : 'publisherId'
    //     }
    // ]

    // belongsTo: [
    //     {
    //         model: 'MA.model.Publisher',
    //         name: 'Note',
    //         primaryKey: 'publisherId',
    //         foreignKey: 'id',
    //         // foreignStore: 'Publishers'
    //     }
    // ]
});
出版商:

Ext.define('MA.model.Publisher', {
    extend: 'Ext.data.Model',

    idProperty: 'id',
    fields: [
        'id',
        'type' ,
        {
            name:'isReceipient',
            type:'boolean'
        },
        'description'
    ]
    // hasMany:  [
    //     {
    //         model: 'MA.model.Note',
    //         name: 'Note',
    //         primaryKey: 'id',
    //         foreignKey: 'publisherId',
    //         // foreignStore: 'Notes'
    //     }
    // ],
});
我走对了吗?我应该使用联想吗?我甚至不能真正理解关联和hasMan/One/belongTo属性之间的区别,我想没有什么区别,只是你声明它的方式

编辑:我的想法是要有一个DataView类,它有一个存储区来保存注释和注释的相应发布者。我有一个主面板:

    items: [
        {
            xtype: 'create-note-panel',
            flex: 1
        },
        {
            xtype: 'notes-panel',
            store: 'Notes',
            flex: 1
        }
    ]
我的笔记面板如下所示:

{
"success": true,
"result": {
    "publishers": [
        {
          "id": "009999",
          "type": "ABC",
          "isReceipient": false,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": true,
          "description": "XYZ"
        },
        {
          "id": 45,
          "type": "ABC",
          "isReceipient": false,
          "description": ""
        }
        ],
        "notes": [
        {
          "publisherId": "009999",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": "46",
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        },
        {
          "publisherId": 45,
          "text": "asdasd",
          "created": "2014-02-23T18:24:06.074Z"
        }
    ]
}
}
Ext.define('MA.view.sections.notes.NotesPanel' ,{
    extend: 'Ext.DataView',
    alias: 'widget.notes-panel',
    // deferInitialRefresh: true,

    itemSelector: 'div.notes-list',
    tpl:  new Ext.XTemplate(
        '<div class="notes-list">',
            '<tpl for=".">',
                '<p>{created}, by {publisherId}</p>',
                '<p>{text}</p>',
                '<hr />',
            '</tpl>',
        '</div>'
    ),

    emptyText: 'No data available',
    initComponent: function() {
        var me = this,
            publisherStore = Ext.data.StoreManager.lookup('Publishers');
        //me.addEvents(   //just messing here, trying stuff
        //    'user-offer-activities'
        //);
        me.callParent(arguments);
    }
    //renderTo: Ext.getBody()

})
;
Ext.define('MA.view.sections.notes.NotesPanel'{
扩展:“Ext.DataView”,
别名:“widget.notes面板”,
//是的,
itemSelector:'div.notes-list',
tpl:新Ext.XTemplate(
'',
'',
“{created},由{publisherId}

”, “{text}

”, “
”, '', '' ), emptyText:“无可用数据”, initComponent:function(){ var me=这个, publisherStore=Ext.data.StoreManager.lookup('Publishers'); //me.addEvents(//只是在这里胡闹,尝试一些东西 //“用户提供活动” //); me.callParent(参数); } //renderTo:Ext.getBody() }) ;

请注意模板中的publisherId。我需要出版商的描述。我不想使用网格,因为这个DataView似乎是一个很好的解决方案,我认为加入两个存储区会很容易,我只是还没弄明白:(

我创建了一个小提琴,它可以让你得到你想要的东西(在视图中显示两个模型的数据)

这是一个有点冗长的方法,因为tpl的工作方式,你没有访问模型的权限,只有模型中的数据。所以我在tpl上创建了一个函数,从基于publisherId的模型中获取我们感兴趣的记录


注意:我创建了fiddle,但没有使用模型之间的任何关联,但是您可能可以从Notes到Publisher创建一个hasOne关联,使用foreignKey链接各个模型中的id和publisherId(尽管我仍然不认为这将使您能够在tpl中直接引用成员).

您想在哪里使用此模型?在网格或表单中!我想根据您的请求创建一个小提琴。我想在DataView中使用它,它有一个存储区,该存储区应该有注释和相应的发布者描述。至少这是我的想法,主要目标是按发布者发布的日期显示所有注释说明。我在这里没有使用任何网格。我在DataView中制作了一个模板,该模板当前连接到Notes存储,但我缺少发布者说明。注意:我将附加DataView类,我将启动我的笔记本。我认为通过使用关联,您的做法是正确的。为了澄清问题:hasMany/hasOne和belongTo a关于3种不同类型的关联。请参阅上的文档(以及“进一步阅读”部分的链接)。您应该尝试:-使用关联的foreignKey和primaryKey属性通过其ID连接模型-或者更改您的json以便嵌套模型的数据。您可以向提供您的工作代码吗?这样我们可以为您提供更多帮助。