PDA

View Full Version : Reading nested JSON list (newbie)



sandor
26 Oct 2010, 3:02 PM
Hey Guys,

i am trying to get the nested components in this type of lists. Two questions (sorry for the newbie attitude, i know i should go deeper with JS, but hey, i am just starting):

1. how can i get the "first_name" field without changing the root to "posts.author"
2. if i have an array i.e. "tags" how to read this array and put this out in the template
3. is there an easy way to chenge the "url: http://www.myurl.de/?json=get_all_posts" to let's say "url: http://www.myurl.de/?json=get_category_posts" with a function? I would like to call this with a button from the top toolbar

JSON (excerpt):
---------------------------


{
"status": "ok",
"count": 7,
"count_total": 7,
"pages": 1,
"posts": [
{
"id": 226,
"type": "post",
"title": "Communication is a Diva",
"title_plain": "Communication is a Diva",
"content": "blahblah",
"excerpt": "blahbal",
"date": "2010-10-20 21:24:37",
"modified": "2010-10-26 01:03:06",
"categories": [
{
"id": 5,
"slug": "articles",
"title": "RealArticles",
"description": "",
"parent": 0,
"post_count": 7
}
],
"tags": [
{
"id": 11,
"slug": "communication",
"title": "communication",
"description": "",
"post_count": 2
},
{
"id": 12,
"slug": "diva",
"title": "diva",
"description": "",
"post_count": 1
},

],
"author": {
"id": 2,
"first_name": "First",
"last_name": "Second",
"nickname": "olaf",
},


My Store and reg model:
---------------------------




Ext.regModel('blog', {
fields: [
{name: 'title', type: 'string'},
{name: 'thumbnail', type: 'string'},
{name: 'id', type: 'string'},
{name: 'content', type: 'string'},
{name: 'excerpt', type: 'string'},
{name: 'modified', type: 'string'},
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'},
{name: 'full', type: 'string'},
{name: 'medium', type: 'string'},
{name: 'large', type: 'string'}
]
});


var store = new Ext.data.Store({
model: 'blog',
proxy: {
type: 'ajax',
url: 'http://www.myurl.de/?json=get_all_posts',
reader: {
type: 'json',
root: 'posts'
}
},
listeners: {
single: true,
datachanged: function(){
Ext.getBody().unmask();
var data = [];

store.each(function(rec){
data.push({
title: rec.get('title'),
excerpt: rec.get('excerpt'),
modified: rec.get('modified'),
thumbnail: rec.get('thumbnail'),
first_name: rec.get('author'),
last_name: rec.get('author.last_name'),
content: rec.get('content')
});
});

}
}
});
store.read();



And finally my template:
---------------------------




tpl: '<tpl for=".">'+
'<div class="story">'+
'<div class="thumbnail"><img src="{thumbnail}" width:"256" height:"256"></div>'+
'<div class="title">{title}</div>'+
'<div class="excerpt">{excerpt}</div>'+
'<div class="modifyed">DATE: {modified} I AUTOR: {first_name} {last_name} </div>'+
'</div>'+
'</tpl>',



Any help is greattly appreciated!

Thanks & Cheers,

Sandor

evant
26 Oct 2010, 3:04 PM
{name: 'first_name', mapping: 'author.first_name'}

sandor
26 Oct 2010, 11:27 PM
THANKS Evan! short, pragmatic and blasing fast answer. working like a charme. I will try to figure out the rest today - but if anybody else has a gracefull minute for me - is highly appreciated.

Cheers,

Sandor