PDA

View Full Version : Ext.data.Store.getAt doesn't work well.



slm_sum
11 Oct 2010, 6:55 AM
Hello Everyone,

I tried to get a record from Ext.data.Store with getAt method,
but it returned 'undefined'.
Plz look at a test code below, and tell me what's wrong.

When I check with Web Inspector, mystore.data exist and seems having correct objects.
I use Sencha Touch 0.96 and Safari 5.0.2.

Test Code:

Ext.setup({
onReady:function(){
// Register a Model.
Ext.regModel('Member',{
fields:[
{name:'member_name', type:'string'},
{name: 'id', type:'string'}
]
}
);

mystore = new Ext.data.Store({
model:'Member',
proxy: {
type: 'ajax',
url: './member.json',
reader:{
type: 'json',
root: 'members',
idProperty: 'id'
}
},
autoLoad: true
});


console.log(mystore.getAt(0)); //this returns undefeined.

}
});

/* JSON
{
"members":[{"id": "1","member_name":"foo"},{"id": "2","member_name":"bar"}]
}

*/

jay@moduscreate.com
11 Oct 2010, 7:39 AM
That's because the request is asynchronous! Look it up on wikipedia. You'll learn a lot.

evant
11 Oct 2010, 1:45 PM
As Jay said, store load is asynchronous, so by the time you call getAt() the call to the server hasn't returned yet. Listen to the load event.