extjs 4 XTemplate 类关联
我正在测试 extjs 4,我偶然发现了一些东西,我似乎无法弄清楚。
我有简单的对象关联:Snapshot - hasMany ->模型
现在,我尝试使用 XTemplate 在视图组件中显示此关联,因此我的 XTemplate 如下所示:
Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<div class="snapshot" id="{id}">',
'<h1>{snapshot}</h1>',
'<p><span class="label">Created: </span>{dateString}</p>',
'<p><span class="label">Models</span></p>',
'<tpl for="models">',
'<p>{name} - {description}</p>',
'</tpl>',
'</div>',
'</tpl>',
'<div class="x-clear bottompad"></div>'
);
我的 JSON 响应如下所示(仅显示“快照”节点):
{
"id": 1,
"snapshot": "Snapshot 1",
"created": 1305806847000,
"models": [
{
"id": 1,
"name": "ABC",
"description": "A B C"
}, {
"id": 111,
"name": "ABCDD",
"description": "A B C XCXC"
}
]
}
正如 extjs 4 引入的模型的概念我已经为快照和模型创建了模型,并根据 API 文档创建了关联。
快照模型:
Ext.define('Snapshot', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
'snapshot',
{name: 'created',type: 'date', dateFormat: 'time' }
],
associations:[
{type: 'hasMany', model: 'Model', name: 'models'}
],
idProperty: 'id'
});
模型模型 ;P
Ext.define('Model', {
extend: 'Ext.data.Model',
belongsTo: 'Snapshot',
fields: [
{ name: 'id', type: 'int' },
{ name: 'snapshot_id', type: 'int' },
'name',
'description'
],
idProperty: 'id'
});
这就是我的问题所在 - 当我使用此设置时,执行 XTemplate 时不会显示任何模型,但是如果我从快照模型中删除关联并仅添加另一个名为“模型”的字段它工作正常。
使用关联时正确显示模型列表的最佳实践是什么? 我是否必须使用嵌套模板和自定义函数来执行此操作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好问题(+1)
看来直接在 XTemplate 中使用关联是不可能的(今天),因为 XTemplate 需要对象数组。 (当你有关联时,这不再是正确的)
你有三个选择 -
提及。 (“听起来”不太好
但会起作用)
.apply
Good Question (+1)
It seems using the associations directly in the XTemplate is not possible (today) because XTemplate expects array of objects. (When you have associations, this is no longer true)
You have three options -
mentioned. (does not 'sound' good
but will work)
.apply
我应该指出,从 4.1 开始,模型记录有一个名为
getData()
的方法,如果使用getData( true )
调用该方法,也将返回关联的数据。I should point out that as of 4.1, a model record has a method called
getData()
, which if called usinggetData( true )
will also return the associated data.我完全同意模板看起来像这样是理想的。然而,实际上很容易获得一个模板来执行您想要的关联操作。模型将其所有字段保存在根级别的称为数据和关联的属性中,并遵循以下约定:associationName+'Store'。因此,您需要做的就是编写模板,如下所示:
I totally agree it would be ideal to have templates look like this. However, it's actually quite easy to get a template to do what you want with associations. Models hold all of their fields within a property called data and associations at the root level with the convention: associationName+'Store'. Therefore, all you need to do is write your template as follows:
您可以侦听 store.load 事件并将关联的数据添加回商店记录,然后模板将起作用(我使用 RowExpander 的 rowBodyTpl 执行此操作)。
You can listen on the store.load event and add the associated data back to the store record, and then the template will work (I did this using the RowExpander's rowBodyTpl).
就像 @Izhaki 所说,在记录上使用 getData(true) 将数据传递到模板,然后执行 @Aaron 所说的变体来循环数据。例如,如果模板是容器的一部分:
此模板片段应该可以正常工作:
Like @Izhaki says use getData(true) on the record to pass the data to the template, then do a variation of what @Aaron says to loop thru the data. For example, if the template is part of a container:
This template snippet should work fine:
根据我最近的经验,如果您不通过商店工作,就不应该遇到问题。
ExtJS 4 文档中的 XTemplate 示例工作正常(至少对我来说)。您可以为这些数据添加模型,示例将继续运行。
我尝试通过一家商店做同样的事情。当您将 store.first().data 传递给 overwrite(...) XTemplate 方法时,关联不在该结构中。
您可以检查以下代码:
您也可以尝试(看看 XTemplate-Store-Associations 的工作有多糟糕)http://www.sencha.com/forum/showthread.php?127320-FIXED-EXTJSIV-242-multiple-HasMany-association-conflict-in-XTemplate。
抱歉没有提供解决方案:(
willy
from my very recent experience, you shouldn't have problems if you are NOT working through stores.
The XTemplate example from ExtJS 4 docs works ok (at least for me). You can add a model for those data and the example will be working on.
I tried to do the same through a store. When you pass the store.first().data to the overwrite(...) XTemplate method, the associations are not in that structure.
You can check in the following code:
You can also can try (to see how bad XTemplate-Store-Associations work) at http://www.sencha.com/forum/showthread.php?127320-FIXED-EXTJSIV-242-multiple-HasMany-association-conflict-in-XTemplate.
sorry not to provide a solution : (
w i l l y