PDA

View Full Version : Cant load data from store



bluesky100
9 Jul 2016, 12:10 PM
Guys I have apache server on Mac, but using this code, I can't load data into store: it prints loaded records, but I can't see records.




// Print store element names
function printStore(store) {
store.each(function(record, index){
console.log(index, record.get("name"));
});
}


Ext.onReady(function(){


/* Define Customer model */
Ext.define('Myapp.model.Customer',{
extend:'Ext.data.Model',
idProperty:'id ',
fields:[
{name: 'id', type: 'int'},
{name: 'name' , type: 'string'},
{name: 'phone' , type: 'string'},
]
});


/* Define store */
Ext.define('MyApp.store.Customers',{
extend : 'Ext.data.Store',
model : 'Myapp.model.Customer' ,
proxy:{
type:'ajax',
url: 'data.json',
reader: { //reader
type:'json', // json type
rootProperty:'records' // root object
}
}
});


// Create store instance
var store = Ext.create("MyApp.store.Customers");


// Load store
store.load(function(records, operation, success) {
console.log('loaded records');

});


printStore(store);

});


Here is json file

{
"success":true,
"id":"id",
"records":[
{
"id": 10001,
"name": "Acme corp2",
"phone": "+52-01-55-4444-3210",
},{
"id": 10002,
"name": "Candy Store LTD",
"phone": "+52-01-66-3333-3895",
}
]
}

Can someone help?

bluesky100
9 Jul 2016, 12:14 PM
I have suspicion I need sleep or smth similar after load... will check now

---


ok printing records inside load callback worked...


seems it had to do load didn't finish when I printed it below

jdkhamba
9 Jul 2016, 12:19 PM
The printstore function needs to be called inside the callback.


store.load(function(records, operation, success) {
console.log('loaded records');
printStore(store);

});