PDA

View Full Version : JsonReader problem when there's a single record



samuelp
22 Mar 2011, 1:27 PM
Hi,
I'm facing the following problem when I try to load data using an AjaxProxy / JsonReader.
My AjaxProxy contains the following reader configuration

{type: 'ajax',
url: myUrl,
reader: {type: 'json', root: 'rootElement.records'}
}
Data gets loaded successfully into my store if the data returned from the server is something like this:


{"rootElement":
{"records": [ {"id": 1, "name": "abc"}, {"id": 2, "name": "def"} ] } }
However, my problem is that when there's a single record, my server app generates the data as follows, without the [ ], and in this case, the store.load() doesn't load the data, as it seems to expect an array.

{"rootElement":
{"records": {"id": 1, "name": "abc"} } }

When there are multiple records, my server app does generate the [ ] and it works fine.

How do I get around this problem when there's only a single record - without having to change the server app? Can the JsonReader be made to look at the single record without having to use an array in the data?
Thanks,
Sam

joelennon
28 Mar 2011, 2:00 AM
It's expecting an array, not an object, so you will need to change your server-side code to output square brackets. I don't believe there's any way of forcing it to read the data the way you have it set up.

This should be relatively straightforward to change on the server-side, is there any particular reason you want to avoid doing so?

interfasys
14 May 2011, 8:06 AM
There is one big reason why the reader should be able to read array-less json responses: Not all public APIs return arrays (think Twitter). So, while we could use a proxy, that means adding an extra point of failure if it's installed solely for that purpose. Of course proxies can add many benefits if you have the knowledge and resources to add and manage one.

Seems this has been discussed at length here:
http://www.sencha.com/forum/showthread.php?125544-Does-EXTJS-4.0-Model-always-have-to-contain-an-Array/page3

and hopefully the same kind of fix will make it to Sencha Touch :)