【发布时间】:2012-09-14 18:04:42
【问题描述】:
考虑一下这个 JSON
{
"stream": {
"posts": [{
"post_id": "1",
"post_type": "text",
"post_thumb": "bla1",
"comments": [{
"comment_id": "7",
"comment_text": "asd",
},
{
"comment_id": "8",
"comment_text": "sdf",
}],
}],
}
}
和我的模特
Ext.define('MyAppApp.model.Post', {
extend: 'Ext.data.Model',
config: {
fields: [
'post_id',
'post_type',
'post_thumb',
'comments',
],
proxy: {
type: 'jsonp',
url: 'http://MyApp.com/home/index_app',
reader: {
type: 'json',
rootProperty: 'stream'
}
}
}
});
以上正确显示了我认为的帖子列表。 我添加了一个控制器来推送一个面板以显示每个帖子的全部内容,这是有效的。
控制器
Ext.define('MyAppApp.controller.Main', {
extend: 'Ext.app.Controller',
config: {
refs: {
stream: 'homepanel'
},
control: {
'homepanel list': {
itemtap: 'showPost'
}
}
},
showPost: function(list, index, element, record) {
/////// begin code block that solved my problem
var data = record.get('comments');
var comments = '';
Ext.Array.each(data, function(item) {
comments += [item.comment_text] + '<br />';
});
/////// end
this.getStream().push({
xtype: 'panel',
html: [
'<p>' + record.get('post_thumb') + '</p>',
'<p>' + record.get('comments') + '</p>',
comments // added to output HTML
].join(''),
scrollable: true,
styleHtmlContent: true,
});
}
});
我的麻烦在于从嵌套在 JSON 中的 comments 检索数据。
使用上面的控制器,我得到了
[object Object],[object Object]
在我的视图和控制台中,我可以打开这些对象并查看我的全部 cmets。
但是如何在视图中显示它们? (例如,如何显示"comment_text"?)
【问题讨论】:
-
我上次编辑了我的帖子,因为如果可能的话,不要在循环中使用 Ext.Array.each 甚至任何函数调用。但这更像是一个最佳实践示例。但我觉得有必要提一下。
标签: javascript json extjs sencha-touch-2