-
24 Oct 2012 5:56 PM #1
Answered: fun with arrays in JSON
Answered: fun with arrays in JSON
Hey community. I haven't been able to figure out how to access part of a piece of data loaded from a store. Here's the JSON:
For the id, title (and other properties omitted here), it's simple, and I get that.Code:entity-list: { "id":"1e3f2071638241e292b705c034be4d6b", "title": "Planets", "entities": { "data": [ { "data": { "name":"Earth", "id":"23f03fe70a16aed0d7e210357164e401", } } }, { "data": { "name":"Moon", "id":"23f03fe70a16aed0d7e210357164e339", } } ] } }
But I'm not sure how to get the "name" part of each of the entities in the list. I've tried several things:Code:config: { fields: [ { name: 'id', type: 'string' }, { name: 'title', type: 'string' }, ...etc ] }
Code:{ name: 'entities', mapping: 'entities.data.data' }, //or entities.dataCode:hasMany: [ { name: 'entities', model: 'KT.model.Entity', associationKey: 'entities' }, //or entities.data ]
For what it's worth, the Entity model looks like this:
What am I missing?Code:Ext.define('KT.model.Entity', { extend: 'Ext.data.Model', config: { fields: [ { name: 'id', type: 'string' }, { name: 'name', type: 'string' }, ] } });
-
Best Answer Posted by siebmanb
I so understand, I never managed to use the associations for models

After several hours of struggling, I decided to explore the data using a DOM debugger (like the one in Safari or Chrome) and use the complete "address" of the data, which can gives something like this :
Dirty but workingCode:Ext.getStore('myStore').getAt(0).data.foo[2].bar[1].value
. If you can get hasMany to work it is much better of course.
-
24 Oct 2012 8:58 PM #2
This code works well to give me exactly what I need from one of the elements, but not both - I feel like I should be using hasMany, but not sure how. This is the furthest I've gottenCode:{ name: 'entities', mapping: 'entities.data[0].data["name"]' },
-
27 Oct 2012 5:19 AM #3Sencha User
- Join Date
- Aug 2011
- Location
- Geneva (CH) - Grenoble (FR)
- Posts
- 250
- Vote Rating
- 14
- Answers
- 11
I so understand, I never managed to use the associations for models

After several hours of struggling, I decided to explore the data using a DOM debugger (like the one in Safari or Chrome) and use the complete "address" of the data, which can gives something like this :
Dirty but workingCode:Ext.getStore('myStore').getAt(0).data.foo[2].bar[1].value
. If you can get hasMany to work it is much better of course.Twitter account @siebmanb.
Co-founder & CEO at ButterflyEffect.
Mobile application developer using Sencha Touch and Phonegap for iOS, Android and webapp.
-
27 Oct 2012 8:17 AM #4
Thanks, I suppose I'll try that out perhaps using 'each' to get all the values, since there is a varying amount across instances of the model. In the meantime, I also have to figure out how to do 'hasOne' for an element that I have to do an additional query for.
-
27 Oct 2012 8:36 AM #5Sencha User
- Join Date
- Aug 2011
- Location
- Geneva (CH) - Grenoble (FR)
- Posts
- 250
- Vote Rating
- 14
- Answers
- 11
One last trick : even if your model does not contain the right entries, most of the time the parameter "raw" in the callback response contains the data you are looking for. Dirty but efficient
Twitter account @siebmanb.
Co-founder & CEO at ButterflyEffect.
Mobile application developer using Sencha Touch and Phonegap for iOS, Android and webapp.
-
27 Oct 2012 9:52 AM #6
Hmm, what if I have a setup like this: a REST call gives me a list of 'entity' models which all work fine. But then, inside each 'entity' model is a 'body' link. The data for it is at '/entity/{id}/body' and I don't know if I should be using a hasOne for this or just some weird custom proxy stuff. The body should be part of the entity, but requires a separate call to be so.
-
28 Oct 2012 3:28 AM #7Sencha User
- Join Date
- Aug 2011
- Location
- Geneva (CH) - Grenoble (FR)
- Posts
- 250
- Vote Rating
- 14
- Answers
- 11
I am not sure I understand. Are you asking if you should send all the entities together, or have a separate call to get the ones you want ?
Twitter account @siebmanb.
Co-founder & CEO at ButterflyEffect.
Mobile application developer using Sencha Touch and Phonegap for iOS, Android and webapp.
-
28 Oct 2012 8:15 AM #8
I'm using a RESTful api, so it does require another call to get certain elements. It's ok though, I found https://github.com/mhorner/RestModel and it looks like that will work a lot better for me. Thanks so much for your help though!
-
28 Oct 2012 11:52 PM #9Sencha User
- Join Date
- Aug 2011
- Location
- Geneva (CH) - Grenoble (FR)
- Posts
- 250
- Vote Rating
- 14
- Answers
- 11
I believe doing another call is not always a bad idea : that way you don't get unnecessary data at first and the request stays light.
Good luck.Twitter account @siebmanb.
Co-founder & CEO at ButterflyEffect.
Mobile application developer using Sencha Touch and Phonegap for iOS, Android and webapp.


Reply With Quote