View Full Version : Handling a Single Json Record

23 Feb 2011, 4:28 AM
JsonStore handles arrays:

images: [
{name: 'Image one', url:'/GetImage.php?id=1', size:46.5, lastmod: new Date(2007, 10, 29)},
{name: 'Image Two', url:'/GetImage.php?id=2', size:43.2, lastmod: new Date(2007, 10, 30)}

However when doing a simple GET request, I don't get an array I just get an object. Can I make a store handle this or is it easier just to use a standard Ajax request?

23 Feb 2011, 4:40 AM
To read a single object, you could configure the JsonReader with:

root: function(v){
return [v];

But do you really need a store to load a single object? What do you need the data for?

23 Feb 2011, 4:46 AM
That's interesting, I never knew root could be a function. In this case the JSON from the server comes back as a single object for displaying one record on the screen.

23 Feb 2011, 4:58 AM
Display how?

You could create a BoxComponent with a tpl, do an Ext.Ajax.request, decode the response and call comp.update(data).

Or you could have a FormPanel with fields and load() it.

23 Feb 2011, 5:16 AM
It's a bit of a weird case maybe as it's a single window that's got both templated data and a form in it, and both of those are powered by data from the same URL.

23 Feb 2011, 5:20 AM
In that case I would load the form and reapply the template with the action.result from the actioncomplete event of the form.

23 Feb 2011, 5:22 AM
Good thinking batman. Thanks for talking this through with me.