-
10 Jan 2012 7:20 AM #1
Answered: Array of JSON data but reader only reading first record
Answered: Array of JSON data but reader only reading first record
Hello all,
I hope someone can help me figure out why my JSON reader attached to a list widget (which contains an array of two items) only loads 1 result. I have a much larger JSON data set (hundreds of "rows"), but I've used a simple one here to illustrate...
This is the JSON
This is the Data Model:Code:{ "event" : [ { "id": { "$t": "hi" } }, { "id": { "$t": "there" } } ] }
And here is my list widget with associated store:Code:Ext.define('HistoryItem', { extend: 'Ext.data.Model', fields: [{name:'name', mapping:'id.$t'}] });
I just don't get it, but when the store loads, it will only return the first item in the JSON. The second is ignored, and my list only displays one item.Code:id: 'HistoryList', xtype: 'list', title: 'Trans History', iconCls: 'star', itemTpl: '{name}', store: new Ext.data.Store ({ model: 'HistoryItem', autoLoad: true, proxy: { type: 'ajax', url: 'places.json', reader: { type: 'json', root: 'event' } }, listeners: { load: function(){ console.log(this.getRange()); } } })
Any clues??
Thanks very much!!,
Ian.
-
Best Answer Posted by mitchellsimoens
This was something I have never seen before but makes sense now. Each Model definition has idProperty set to 'id' and since you were using 'id' in your response it was trying to use that field as the id of that record. Both records were loaded except there was an index of 0 and -1 so therefor it looked like there was only one. I changed the idProperty on the Model definition to something else and it worked no problem:
Code:Ext.define('HistoryItem', { extend : 'Ext.data.Model', idProperty : 'nameId', fields : [ { name : 'name', mapping : 'id.$t' } ] });
-
10 Jan 2012 8:22 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,654
- Vote Rating
- 434
- Answers
- 3107
This was something I have never seen before but makes sense now. Each Model definition has idProperty set to 'id' and since you were using 'id' in your response it was trying to use that field as the id of that record. Both records were loaded except there was an index of 0 and -1 so therefor it looked like there was only one. I changed the idProperty on the Model definition to something else and it worked no problem:
Code:Ext.define('HistoryItem', { extend : 'Ext.data.Model', idProperty : 'nameId', fields : [ { name : 'name', mapping : 'id.$t' } ] });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
10 Jan 2012 8:37 AM #3
Hi Ian,
to solve this issue I suggest you to set the "record" config to "id" on your store reader and change the field mapping as follows:
And everything will works fine!Code:Ext.setup({ onReady: function() { Ext.define('HistoryItem', { extend: 'Ext.data.Model', fields: [ {name:'name', mapping:'$t'} ] }); var store = Ext.create('Ext.data.Store', { model: 'HistoryItem', autoLoad: true, proxy: { type: 'ajax', url: 'places.json', reader: { type: 'json', root: 'event', record: 'id' } }, listeners: { load: function(){ console.log(this.getRange()); } } }); var list = Ext.widget('list', { fullscreen: true, title: 'Trans History', iconCls: 'star', itemTpl: '{name}', store: store }); } });
Hope this helps.Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata
-
10 Jan 2012 12:25 PM #4
'id' property in the JSON
'id' property in the JSON
Thanks very much for you help guys! I spent hours on this problem last night and just couldn't make it work.
Setting the idProperty to something different so it doesn't get confused with my JSON 'id' property is something I will definitely keep in mind in the future!
I'm sure it's not the only JSON with an 'id' property in it!!
Thanks again!!
-
6 Feb 2012 8:37 AM #5
I'm having this same problem on ST 1.x. The fix doesn't seem to directly translate over, is there any chance someone could post the 1.x equivalent?


Reply With Quote