-
19 Sep 2012 3:48 PM #1
Unanswered: How to pass JSON of a post with multiple comments to a view?
Unanswered: How to pass JSON of a post with multiple comments to a view?
I am successfully showing a series of blog posts such as
by using thisCode:{ "stream": { "posts": [{ "post_id": "1", "post_thumb": "pic", "post_text": "bla1", }, { "post_id": "2", "post_thumb": "pic", "post_text": "bla2", }] } }
When a user taps to view the post he is taken to a panel that shows it. In that panel I'd like to show all comments in that post, but I'm unsure how the JSON should be formed for this.Code:Ext.define("MyApp.view.Home", { extend: 'Ext.navigation.View', xtype: 'homepanel', requires: [ 'Ext.dataview.List', 'Ext.data.proxy.JsonP', 'Ext.data.Store', ], config: { items: { xtype: 'list', itemTpl: [ '<img src="{post_thumb}" />', '<p>{post_text}</p>' ].join(''), store: { autoLoad: true, fields: [ 'post_id', 'post_text', 'post_thumb' ], proxy: { type: 'jsonp', url: 'http://me.com/home/index_app', reader: { type: 'json', rootProperty: 'stream' } } } } } });
Would it be something like this? Or does this JSON need a different format?
Code:{ "stream": { "posts": [{ "post_id": "1", "post_type": "text", "post_thumb": "bla1", "comment": [{ "comment_id": "7", "comment_text": "asd", }, { "comment_id": "8", "comment_text": "sdf", }], }], } }
-
21 Sep 2012 4:14 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,637
- Vote Rating
- 435
- Answers
- 3106
That JSON is fine, since you aren't using associations then you can simply use rec.get('comments') to get the comments array.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
21 Sep 2012 3:31 PM #3
Thanks Mitchell - I don't seem to be able to loop through the comments though.
I use this controller to show the post and comments on tap:
but I get this below the postCode:Ext.define('MyApp.controller.Main', { extend: 'Ext.app.Controller', config: { refs: { stream: 'homepanel' }, control: { 'homepanel list': { itemtap: 'showPost' } } }, showPost: function(list, index, element, record) { this.getStream().push({ xtype: 'panel', title: record.get('post_author_name'), html: [ '<img src="' + record.get('post_thumb') + '" />', '<p>' + record.get('post_text') + '</p>', '<p><img src="' + record.get('picture') + '" width="30" />' + record.get('post_author_name') + ' posted ' + record.get('timestamp') + '</p>', '<p>' + record.get('comments') + '</p>' ].join(""), scrollable: true, styleHtmlContent: true }); } });
Code:[object Object],[object Object] // there are 2 comments in JSON, BTW
-
25 Sep 2012 6:18 AM #4
Instead of html: you should use tpl: with XTemplate using <tpl for> to loop then feed your record in the data: config.


Reply With Quote