PDA

View Full Version : Store only ever has last record and not all the records



wxkevin
19 Feb 2013, 6:56 AM
I have JSON being returned in this format:


{ "Users": {
"User": {
"id": "1",
"fname": "John",
"sex": "Male"
},
"User": {
"id": "2",
"fname": "Mary",
"sex": "Female"
}
}}

Here is my Model and Store:


Ext.define('ME.model.Users', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'string'},
{name: 'fname', type: 'string},
{name: 'sex', type: 'string'}
],
proxy: {
type: 'ajax',
url: '/some/path/users',
reader: {
type: 'json',
root: 'Users.User'
}
}
});


Ext.define('ME.store.UsersStore', {
extend: 'Ext.data.Store',
requires: ['ME.model.Users'],
model: 'ME.model.Users',
autoLoad: true
});

I load the store into a grid. The grid always shows the last record in the returned data. In the case above it would only show the "Mary" record. I have tried several with several different URLs that just happen to return data in the same format and it always returns the last record only.

I am using Ext JS 4.1.1. Suggestions?

vadimv
19 Feb 2013, 7:02 AM
Your reader root points to an object, which is not an array. Not sure what you are trying to achieve, but probably I would guess that if you set the root like this:


reader:{
root: 'Users',
type: 'json'
}

and json like :



{ "Users": [{
"id": "1",
"fname": "John",
"sex": "Male"},
{
"id": "2",
"fname": "Mary",
"sex": "Female"
}]}
will be what you expect

wxkevin
19 Feb 2013, 7:35 AM
I see what you mean. Unfortunately I don't have any control over the returned JSON.

Is there anything else I can do?

vadimv
19 Feb 2013, 7:58 AM
Probably to make your custom reader.
Also note that in javascript when is decoded such json, with properties with the same name on same chain hierarchy, then the return will be the last property within that chain (object)...you can try in browser:


Ext.decode('{"Users":{ "user":{"id":35}, "user":{"id":34}}}')

That's the reason of what you get, btw.