gpt4 book ai didi

extjs - 通过 ID 连接两个模型

转载 作者:行者123 更新时间:2023-12-01 16:23:53 24 4
gpt4 key购买 nike

几天前我问了一个 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"
}
]
}
}

所以我得到两个数组,publishers 和 notes。我有两个模型,我通过 Controller 使用 loadRawData() 将它们加载到模型中,它有效,我在商店中得到了我所有的出版商和笔记。 (他们都有一家商店——Publishers and Notes)。但是我需要在注释中使用 publisherId 来显示发布者描述。我尝试了很多我可以使用 google 和 sancha 文档找到的东西:associations、hasmany、hasone、belongsto 并创建了由两个聚合模型组成的第三个商店。到目前为止没有任何效果。

我想要的是拥有一个包含所有笔记的商店,而且所有笔记都有发布者信息。

我将在下面复制我的两个模型,您可以在那里看到,注释掉我一直在尝试的内容。我还尝试更改 ID、名称等,所以这些的变体。但我永远无法让 Notes 包含发布者的信息。

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
}
]

我的笔记面板看起来像这样:

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()

})
;

注意模板中的 publisherId。我需要那里的出版商描述。我不想使用网格,因为这个 DataView 似乎是一个很好的解决方案,我认为加入两家商店会很容易,但我还是想不通:(

最佳答案

我创建了一个 fiddle 来实现您想要的结果(在 View 中显示来自两个模型的数据)。

虽然这是一种有点冗长的方法,但由于 tpl 的工作方式,您无法访问模型,只能访问其中的数据。所以我在 tpl 上创建了一个函数,它根据 publisherId 从模型中获取我们感兴趣的记录。

https://fiddle.sencha.com/#fiddle/o9o

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

关于extjs - 通过 ID 连接两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com