PDA

View Full Version : Read special JSON into store



clifficious
17 May 2013, 5:34 AM
Hi,

I'm totally stuck at a point for a while now. Maybe I'm just too stupid to understand the idea behind the stores. I have following Json string

{ "1":[0,68,65,75],
"2":[68,0,9,23],
"3":[232,343,9,24],
"4":[33,24,44,543]
}

And my store looks like this.

Ext.define('MyStore.Adjacencies', {
extend: 'Ext.data.Store',
storeId: 'ThemeAdjacencies',

fields: [
{
name: 'theme'
}],

proxy: {
type: 'ajax',
url: 'data/Matrix.json',
reader: {
type: 'array',
}
}
});

But unfortunately this only reads the first line of the json. Is there an option to read the complete data?

Thanks for any hints and helps.

tobiu
17 May 2013, 5:59 AM
take a look at:
http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.data.reader.Json
(example on top)

most of the time, i return a JSON string like:



{
"success":true,
"data": [
{
"theme":...
},
{
"theme":...
}
]
}


the important part is, that you specified "theme" as a field inside your model, so the reader will search for this field in the response.

best regards
tobiu

clifficious
17 May 2013, 6:04 AM
Hi Tobi,

I already tried changing the reader to JSON. But that just looks for the property 'theme' as intended. Unfortunately I can not change the JSON file. So I really need to read the above given JSON.
The trick is, that it does not have a constant property to read. But that the property ("1", "2",...) is changing.

tobiu
17 May 2013, 8:34 AM
if you can not change the JSON response, you will need to create a custom reader, which is a bit tricky but possible.

i recommend to extend the json one:
http://docs.sencha.com/extjs/4.2.0/source/Json2.html#Ext-data-reader-Json

especially take a look at:


extractData: function(root) {
var recordName = this.record,
data = [],
length, i;

if (recordName) {
length = root.length;

if (!length && Ext.isObject(root)) {
length = 1;
root = [root];
}

for (i = 0; i < length; i++) {
data[i] = root[i][recordName];
}
} else {
data = root;
}
return this.callParent([data]);
}

clifficious
20 May 2013, 11:22 PM
Hi Tobi,

thanks for the hint. I'll give it a try.