PDA

View Full Version : Data.store load delay



cnicolas
17 Nov 2009, 3:01 AM
I have a problem whith this simple code :



Ext.onReady(function(){

var MyStore=new Ext.data.Store({
proxy:new Ext.data.HttpProxy({url:'query.php',method:'POST'}),
reader:new Ext.data.JsonReader(
{root:'results'},[
{name:'parameter',type:'string',mapping:'parameter'},
{name:'value',type:'string',mapping:'value'},
])
});

var MyColModel=new Ext.grid.ColumnModel([
{header:'parameter',dataIndex:'parameter',width:100},
{header:'value',dataIndex:'value',width:100}
]);

var MyGrid=new Ext.grid.GridPanel({
el:'MyGrid',
store:MyStore,
cm:MyColModel,
autoHeight:true,
width:200
});

MyStore.load();
MyGrid.render();

alert(MyStore.getCount());

});
query.php return :
({"results":[
{"parameter":"param1","value":"1"},
{"parameter":"param2","value":"2"},
{"parameter":"param3","value":"3"}
]})


I wish to know the number of rows returned by php (getCount on the data.store).
But I obtain always 0, pehpas because getCount is launched before data is loaded.
How I can add any wait method to obtain the correct count ?

Thanks for your help

17 Nov 2009, 4:56 AM
You need to learn about asynchronous programming and events.

myStore.on('load', funciton() {
alert(MyStore.getCount());
});

Mike Robinson
17 Nov 2009, 9:28 AM
Welcome to "AJAX = Asynchronous JavaScript."

Categorically speaking, whenever you ask anything, anywhere, to do something, your code does not wait for the completion of that request! Instead, you specify a callback that is to be invoked when (and if...) the expected completion occurs.

You do not know when that will be. You do not know if that will be. All that you know is that, if-and-when something happens, the appropriate callback will be invoked.

So... when you ask a store to "load," the wheels are put in motion, and from that point forward you're just like the hopeful job-applicant 8-| ... "we'll call you ..."

cnicolas
19 Nov 2009, 4:40 AM
OK, I have the solution.
Thanls for your help...