PDA

View Full Version : Simple question regarding JSON, Ext.data.JsonStore and records.



thospfuller
24 Apr 2008, 1:47 AM
Hi,

I'd like to say thanks in advance for help with this issue.

I am using an Ext.data.JsonStore to get information pertaining to how the column on a grid should be ordered, as well as visible/hidden. What I have works, however I'm not completely satisfied with the solution since there's a step I need to go through in order to make this work and I'm not sure this is necessary -- so, I'd like a second opinion.

The issue is that once the JSON has been loaded, the records have this information under, for example, recs[ctr].json.displayName, however in order to get the ColumnModel to render, this information needs to be under recs[ctr].data.displayName. My solution is to iterate through the records and copy this information to the appropriate place, as follows:


for

(ctr = 0; ctr < recs.length; ctr++) {

recs[ctr].data.displayName = recs[ctr].json.displayName;
recs[ctr].data.hidden = recs[ctr].json.hidden;
...
}


Is this entirely necessary? I suspect there's a better way to do this, so I'd like to hear what the experts have to say...


Best,


Tom

Animal
24 Apr 2008, 1:59 AM
Well, I haven't seen the object literal which provides data for this Store, but at a guess:

Use the mapping in your Record definition: mapping: 'json.displayName'

thospfuller
24 Apr 2008, 3:34 AM
Hi Animal,

Yes the mapping is what I was missing and my initial tests indicate that it appears to solves this problem.

Thanks for your help,

Tom

For anyone else with a similar question, what I had before was:

var dataStore = new Ext.data.JsonStore({
url: 'my.action',
root: 'results',
baseParams: { type : datasourceName }
});
...
for (ctr = 0; ctr < recs.length; ctr++) {
recs[ctr].data.displayName = recs[ctr].json.displayName;
recs[ctr].data.hidden = recs[ctr].json.hidden;
recs[ctr].id = recs[ctr].json.name;
}

and after:

var dataStore = new Ext.data.JsonStore({
url: 'my.action',
root: 'results',
baseParams: { type : datasourceName },
fields: [
{name : 'displayName', mapping : 'displayName'},
{name : 'hidden', mapping : 'hidden'},
{name : 'id', mapping : 'name'}]
});

Animal
24 Apr 2008, 4:05 AM
So, you've changed the format of your returned JSON to remove the json object which wrapped your individual data items?

thospfuller
24 Apr 2008, 4:30 AM
So, you've changed the format of your returned JSON to remove the json object which wrapped your individual data items?

Animal,

No, I haven't changed the JSON at all. Here's an example of the JSON I'm returning from the server:

{'results' : [
{'displayName' : 'Card No', 'hidden' : false, 'name' : 'm_cardref'},
{'displayName' : 'Molecule', 'hidden' : false, 'name' : 'm_chemistry'}
...]}

Using the mapping:

{name : 'displayName', mapping : 'displayName'}

does appear to set the value of the record's json.displayName on the data.displayName attribute, which is what I think I want. I need to set the column to hidden and record.hidden = record.json.hidden will work. I suspect that I'm required to set this attribute manually, however feel free to correct me if I'm wrong.

What I'm trying to accomplish is the following: if a user changes the ordering or visibility of the columns on a grid, I need to persist this state on the server so that when the user returns to this same page, the visibility and ordering will appear as they've previously configured on the grid.

I do have most of this working, however I'm exploring ways of making the solution more elegant, since I'm not an expert JavaScript developer.

Tom

Animal
24 Apr 2008, 4:34 AM
OK, I see. The mapping wasn't working, so you were doing the job of the reader manually.

You do not need to do anything to create the fields in the Records. It is the job of the Reader to use the mapping to extract the data from each element of the record array.

You can add a converter function if you need but postprocessing the Store should not be necessary.