PDA

View Full Version : Store/Model help - cant understand how to read the json structure



gkatz
9 Aug 2011, 7:44 AM
Hi;
I have the following JSON structure (validated with jsonlint):

{
"one": [
{
"date": "2011-06-26 14:51:00.0",
"min": 0.91,
"max": 1.49
},
{
"date": "2011-06-27 14:51:00.0",
"min": 0.91,
"max": 1.54
}
],
"two": [
{
"date": "2011-06-26 14:51:00.0",
"min": 0.91,
"max": 1.49
},
{
"date": "2011-06-27 14:51:00.0",
"min": 0.91,
"max": 1.54
}
]
}
I cant seem to understand what model and store configuration I need to use.
thanks in advance.

jjerome
9 Aug 2011, 8:18 AM
Ext.regModel('dates', {
fields: [{name: 'date', id: 'date'}, //id's really aren't needed
{name: 'min', id: 'min'},
{name: 'max', id: 'max'} ] });

var myStore = new Ext.data.Store({
model: 'User', //here, if your data is local (in the project somewhere)
//use the line of code below
data: 'filepathtodata.json',
//if you need to retrieve it from somewhere else, use a proxy (either xml or json)
proxy: {
type: 'ajax',
url : 'serverpath/dates.json', //path to the .json data file
reader: {
type: 'json', //in your case it is json, not xml
root: 'one' //this would have to be either the 'one' or 'two' you have
//as your roots/records in your json data
}
},
autoLoad: true //copied from the Store example on API docs });


I hope that helps a little. Don't spot me on the code because I've only used XML proxies and data so far, not JSON :)

gkatz
9 Aug 2011, 8:21 AM
this is exactly my problem.
I can only have one or two but not both...
I assume from your answer that its not possible to have all the data fit into a model due to the 'dual root' you mentioned?

jjerome
9 Aug 2011, 8:40 AM
What I use currently is the same root, but I can retrieve and read from multiple data structures with the same root. So I think that you are correct with your assumption. I've never successfully read from multiple roots before.

I suggest building your json data differently where there is only one root.

Maybe a solution will pop up in the future :)

EDIT: I was going through the API and I saw this configuration of json data. Maybe it could help a little?



// store with data
var data = {
text: 'Groceries',
items: [{
text: 'Drinks',
items: [{
text: 'Water',
items: [{
text: 'Sparkling',
leaf: true
},{
text: 'Still',
leaf: true
}]
},{
text: 'Coffee',
leaf: true
},{
text: 'Espresso',
leaf: true
},{
text: 'Redbull',
leaf: true
},{
text: 'Coke',
leaf: true
},{
text: 'Diet Coke',
leaf: true
}]
},{
text: 'Fruit',
items: [{
text: 'Bananas',
leaf: true
},{
text: 'Lemon',
leaf: true
}]
},{
text: 'Snacks',
items: [{
text: 'Nuts',
leaf: true
},{
text: 'Pretzels',
leaf: true
},{
text: 'Wasabi Peas',
leaf: true
}]
},{
text: 'Empty Category',
items: []
}]
};
Ext.regModel('ListItem', {
fields: [{name: 'text', type: 'string'}]
});
var store = new Ext.data.TreeStore({
model: 'ListItem',
root: data,
proxy: {
type: 'ajax',
reader: {
type: 'tree',
root: 'items'
}
}
});

cited: http://dev.sencha.com/deploy/touch/docs/?class=Ext.NestedList