PDA

View Full Version : store.loadData() doesn't load the JSON data



aryanet
16 Jun 2012, 11:33 AM
Hi,
I am trying to load a JSON data which is replied back by an AJAX request to a grid.
My store definition:


Ext.define('Report.store.CustomerDataStore', {
extend: 'Ext.data.Store',
requires: [
'Report.model.Customer'
],

constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: false,
storeId: 'CustomerDataStore',
model: 'Report.model.Customer',
proxy: {
type: 'ajax',
url: '',
reader: {
type: 'json',
root: 'data',
record: 'fields'
}
}
}, cfg)]);
}
});


There is button in my app which is defined as follows:


xtype: 'button',
handler: function(button, event) {
var queryform = this.up('form').getForm();
var me = this;
if(queryform.isValid())
{
Ext.Ajax.request({
url: 'customers/', // where you wanna post
success: function(response){
var mystore = Ext.data.StoreManager.lookup('CustomerDataStore');
var myData = Ext.JSON.decode(response.responseText);
console.log(myData.toSource());
mystore.loadData(myData);

},
jsonData : Ext.JSON.encode(queryform.getValues())
});
}
},


The problem is that my grid doesn't show the replied data! I am sure that my replied JSON format is OK. I have checked it with a json file. also myData.toSource() returns my desired JSON format.
I am pretty confused what I am doing wrong?

Could you plz help?

skirtle
16 Jun 2012, 1:25 PM
Seems a bit odd to load the data directly like that rather than going through the proxy but it should be possible.

Could you post a sample of the JSON being returned by your server and the definition for your model?

aryanet
16 Jun 2012, 10:44 PM
My JSON is as follows:


{"data":[
{
"pk": 1,
"model": "customer.customer",
"fields": {
"national_id": 123,
"cellphone": 123,
"name": "A",
"family": "M2",
"city": "",
"credit": 123,
"country": "",
"age": 4,
"address": "",
"creation_time": "2012-05-29T19:46:20.606Z",
"main_customer": [
{
"pk": 1,
"model": "customer.customer",
"fields": {
"national_id": 123,
"cellphone": 123,
"name": "A",
"family": "M2",
"city": "",
"credit": 123,
"country": "",
"age": 4,
"address": "",
"creation_time": "2012-05-29T19:46:20.606Z",
"main_customer": [
1,
2
],
"phone": 123,
"job": "hh",
"birthday": "2012-05-30",
"passport": 2,
"overseer": null,
"creator": 1,
"email": ""
}
},
{
"pk": 2,
"model": "customer.customer",
"fields": {
"national_id": 1232,
"cellphone": null,
"name": "123",
"family": "123",
"city": "",
"credit": null,
"country": "",
"age": 123,
"address": "",
"creation_time": "2012-05-29T19:54:23.107Z",
"main_customer": [
1
],
"phone": null,
"job": "",
"birthday": null,
"passport": null,
"overseer": null,
"creator": 1,
"email": ""
}
}
],
"phone": 123,
"job": "hh",
"birthday": "2012-05-30",
"passport": 2,
"overseer": null,
"creator": 1,
"email": ""
}
},
]}

evant
16 Jun 2012, 10:48 PM
loadData doesn't go through the reader, so you would pass:



store.loadData(myData.data);


Or, use the loadRawData method.

aryanet
16 Jun 2012, 11:01 PM
Great, loadRawData worked prefect for me.
Thanks for your help Evant.