PDA

View Full Version : JSON not loading in list



waftock
25 Mar 2011, 1:22 PM
Hello,

I'm getting json from a mysql query, the result looks like this:


[{"id":"17","place_id":"17","user_id":"2","group_id":null,"order":"1","category_id":"6","name":"Onze-Lieve-Vrouwekerk","address":"Onze-Lieve-Vrouwestraat 1, Kortrijk","lat":"50.829354","lng":"3.267086","description":"blabla","phone":"","url":"http:\/\/www.kortrijk.be\/adressen\/onze-lieve-vrouwekerk"},{"id":"24","place_id":"24","user_id":"2","group_id":null,"order":"2","category_id":"8","name":"Zwembad Mimosa","address":"Mimosalaan, Kortrijk","lat":"50.816022","lng":"3.283727","description":"blabla","phone":"056 27 80 70","url":""}]


Now I have one model and one store for this data!

this is the MapStore.js file:

STICK.stores.mapstore = new Ext.data.Store({
model: 'MapModel',
proxy: {
type: 'ajax',
url: 'http://localhost:80/2DEV1/MA4_STICK/flash/amfphp/json.php/StickService.getStepsByUserId/2',
reader: {
type: 'json'
},
autoLoad: true
}
});

and this is the MapModel.js file:

Ext.regModel('MapModel', {
fields:[{name: 'id', type: 'int'},
{name: 'place_id', type: 'int'},
{name: 'user_id', type: 'int'},
{name: 'group_id', type: 'int'},
{name: 'order', type: 'int'},
{name: 'category_id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'address', type: 'string'},
{name: 'lat', type: 'double'},
{name: 'lng', type: 'double'},
{name: 'description', type: 'string'},
{name: 'phone', type: 'string'},
{name: 'url', type: 'string'}]
});

Then in my the js file where i need the store and the data I have this:

STICK.views.MapPanel = Ext.extend(Ext.Panel, {
title: 'Map',
cls: 'map tab',
layout: 'card',
initComponent: function(){
this.items = this.buildItems();

STICK.views.MapPanel.superclass.initComponent.apply(this, arguments);
},
buildItems: function(me){

var map = new Ext.List({
store: STICK.stores.mapstore,
itemTpl: '{id} ({name})',
onItemDisclosure: function(record) {
console.log(record.data.id + record.data.name);
}
});

return map;
}
});

Ext.reg('stick-mappanel', STICK.views.MapPanel);

I'm just trying to retrieve the id and the name from the store but when i check the console (STICK.stores.mapstore.data.items) it returns nothing, just []. I guess my store is empty, actually I'm pretty sure but I dont know why its empty?

Any suggestions? I would really appreciate it!

bobvandell
25 Mar 2011, 1:29 PM
This is a double post but here's your answer again:

The autoLoad config option should be in the store declaration, not the proxy.

waftock
25 Mar 2011, 1:33 PM
Hello,

I just pressed the preview thread button and instead of previewing it, it was posted. Anyway, I've done what you said but that didn't solve the problem, any other ideas what might be wrong in this code? I really appreciate your help =D>!

waftock
25 Mar 2011, 1:48 PM
Uncaught Ext.data.JsonReader.getResponseData: JSON object not found

I do have a new error in my console now... I know what the error means but can't figure out where it's coming from?

bobvandell
25 Mar 2011, 1:56 PM
It did solve that problem, but now you have a new one. That error means you're not returning the JSON data in the proper format.

Try wrapping your array in an object so you can assign your array to a property of that object.

Then in your reader configuration declare the property name of your root object.

Your client code:



STICK.stores.mapstore = new Ext.data.Store({
autoLoad: true,
model: 'MapModel',
proxy: {
type: 'ajax',
url: 'http://localhost:80/2DEV1/MA4_STICK/flash/amfphp/json.php/StickService.getStepsByUserId/2',
reader: {
type: 'json',
root: 'record_set'
}
}
});



Your mysql data:



{"record_set":[{"id":"17","place_id":"17","user_id":"2","group_id":null,"order":"1","category_id":"6","name":"Onze-Lieve-Vrouwekerk","address":"Onze-Lieve-Vrouwestraat 1, Kortrijk","lat":"50.829354","lng":"3.267086","description":"blabla","phone":"","url":"http:\/\/www.kortrijk.be\/adressen\/onze-lieve-vrouwekerk"},{"id":"24","place_id":"24","user_id":"2","group_id":null,"order":"2","category_id":"8","name":"Zwembad Mimosa","address":"Mimosalaan, Kortrijk","lat":"50.816022","lng":"3.283727","description":"blabla","phone":"056 27 80 70","url":""}]
}

waftock
25 Mar 2011, 2:22 PM
Okay, so far so good, all the errors are gone... It still won't show up in the console or in the list so I must be doing something wrong there but hopefully I'll figure it out! Thanks for all your help, and if you can see what's wrong, please let me know ;)!

waftock
25 Mar 2011, 3:04 PM
Don't know why but I'm pretty sure the json data is still not loaded into my store, frustrating ;)!

bobvandell
25 Mar 2011, 3:24 PM
Try using the successProperty in your reader as well.

waftock
25 Mar 2011, 3:46 PM
Looks like that's working "success" so the data is actually loaded but I can't read it, is there something wrong in my MapModel then? I'll keep looking for the solution of course, thanks for all your help so far!

waftock
26 Mar 2011, 1:13 AM
You will never believe what my problem was...


STICK.stores.mapstore = new Ext.data.Store({
model: 'MapModel',
proxy: {
type: 'ajax',
url: 'http://localhost:80/2DEV1/MA4_STICK/flash/amfphp/json.php/StickService.getStepsByUserId/2/',
reader: {
type: 'json'
},
autoLoad: true
}
});

That's right, I'm pretty sure you can't see what I've changed, but there is another / after the url! Fixed the problem. Thanks bobvandell, you also helped me remove some mistakes in the project.

optiplex
26 Mar 2011, 3:53 AM
it is very difficult situation you will share

optiplex
26 Mar 2011, 3:55 AM
you will share our problem and mark the all issue.

bobvandell
31 Mar 2011, 12:23 PM
Glad to help!