PDA

View Full Version : How to know when a store is created?



JoelB
8 Jun 2012, 8:09 AM
Hi,

I am creating a store without data in it and later supply both data and model. But there seems to be a race condition between creating the store and setting the data. If I set the data and model right after in the store doesnt seem to understand it, but if I do a setTimeout 3000 in which I set the same data and model it works.

Is there some kind of event I could listen to, in order to know when the store is done creating itself?

This is the working code, to make it break. just remove the setTimeout and let the code run.


var myStore = Ext.create('Ext.data.Store', {
autoLoad: true,
autoSave: true,
fields: [],
proxy: {
type: 'memory',
reader: {
type: 'json',
},
writer: {
type: 'json',
}
}
});
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'},
{name: 'type', type: 'string'}
]
});


setTimeout(function(){
myStore.loadRawData([
{
name: "foo",
age: 33,
type: "human"
}, {
name: "bar",
age: 33,
type: "human"
}, {
name: "affe",
age: 1,
type: "apa"
} ]);


myStore.getProxy().getReader().setModel("User");


//lets see if the store is populated
console.log(myStore.data.items);


}, 3000);




Cheers,
//J

JoelB
8 Jun 2012, 8:14 AM
Actually that code works in the console without setTimeout but if you write the same console.log statement again manually (that is a little later) the array is empty.

Something in the lifecycle empties the store.data.items array!

EDIT: Seems Autoload=true was a major Fail. It also clears the store and makes no sense in general. :)

Anyone have a clue?
//J