PDA

View Full Version : JSONStore - Cannot access to data



Pacos
1 Sep 2011, 1:00 AM
Hello,

I'm stuck on this issue for two days now, and I'm currently out of ideas to try out.

Here is the issue :

I have a JSONStore :



var playerStore = new Ext.data.JsonStore({
//autoDestroy: true,
storeId: 'playerStore',
proxy: {
type: 'ajax',
url: 'Controller?action=plist',
reader: {
type: 'json',
root: 'players',
idProperty: 'name'
}
},
fields: ['name']
});
playerStore.load({scope : this,
callback: function(records, operation, success) {
console.log(records);
}
});

Controller?action=plist sends this piece of JSON (validated with JSLint) :



{"players":[{"name":"Anna"},{"name":"Bob"},{"name":"Eve"},{"name":"John"}]}

I can also bind this store with a comboBox, which shows correctly the names above.

BUT I cannot access the data with the following JSONStore methods : getAt(), find(), findBy(), last(), etc. In fact, I haven't been able to access the data no matter what I tried.

If I try to do this :



var test = playerStore.getAt(1).get("name");

In Firebug I get the following error :


playerStore.getAt(1) is undefined

If I try to "find()" a record, it always sends back -1.

Do you have any idea/suggestion ? I put a callback on the store.load(), and I can see the records showing up in Firebug, but I wouldn't be able to tell if everything went fine by reading all the details of the trace : my comprehension ability stops here.

Thanks for your much needed help !

aacoro
1 Sep 2011, 2:02 AM
perhaps a scoping issue...
Do you see the store when you try this:



console.log(playerStore);


Otherwise try this:

Ext.get('playerStore').getAt(1).get('name');

Pacos
1 Sep 2011, 2:11 AM
Nope, not a scoping issue. Thanks anyway for your answer.

In fact, I read that placing a callback on the store.load() function somehow forced this call to become synchronous. I'm still not sure about that, because it didn't seem to work.

In fact, it's logical that I couldn't access the store's data after the load, because this function remained asynchronous. Thus the store was not loaded yet when I tried to access its content.

But I found another (certainly cleaner) solution : I put a listener on the load event of the store, and accessed the store's data from here. Works perfectly !

For those interested, here is the code :


var test;

var playerStore = new Ext.data.JsonStore({
autoDestroy: true,
storeId: 'playerStore',
proxy: {
type: 'ajax',
url: 'Controller?action=plist',
reader: {
type: 'json',
root: 'players',
idProperty: 'name'
}
},
fields: ['name'],
listeners: {
'load': function() {
test = playerStore.getAt(0).get('name');
}
}
});



Edit : I don't know how to mark this thread as 'Answered' (if I can).