PDA

View Full Version : easy q: how do I import data to store from a json file



ahsec
14 Aug 2012, 2:07 AM
the store :


Ext.define('MyApp.store.b_riCountries', { extend: 'Ext.data.Store',


constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'b_riCountries',
proxy: {
type: 'ajax',
url: 'Htmls/countries.json',
reader: {
type: 'json',
root: 'countries'
}
},
fields: [
{
name: 'Country'
}
]
}, cfg)]);
}
});

the json file :


{ countries:[
{Alias:'AP',Country:'Asia/Pacific Region'},
{Alias:'EU',Country:'Europe'},
{Alias:'AD',Country:'Andorra'},
{Alias:'AE',Country:'United Arab Emirates'},
{Alias:'AF',Country:'Afghanistan'},
{Alias:'AG',Country:'Antigua and Barbuda'},
{Alias:'AI',Country:'Anguilla'},
{Alias:'AL',Country:'Albania'},
{Alias:'AM',Country:'Armenia'},
{Alias:'CW',Country:'Curacao'},
{Alias:'AO',Country:'Angola'},
{Alias:'AQ',Country:'Antarctica'},
{Alias:'AR',Country:'Argentina'},
{Alias:'AS',Country:'American Samoa'},
{Alias:'AT',Country:'Austria'},
{Alias:'AU',Country:'Australia'},
{Alias:'AW',Country:'Aruba'},
{Alias:'AZ',Country:'Azerbaijan'},
{Alias:'BA',Country:'Bosnia and Herzegovina'},
{Alias:'BB',Country:'Barbados'},
{Alias:'BD',Country:'Bangladesh'},
{Alias:'BE',Country:'Belgium'},
.
.
.
.
]
}



any ideas why it doesn't work??

I have a grid with Country as the data index...nothings shows up...I know how to do it inline but I need to use a file


Thanks

ezriel
14 Aug 2012, 4:10 AM
try to add "Alias" field


fields:[
{name: 'Alias", type:'string'},
{name:'Country',type:'string'}
]


or define Model


Ext.define('Country', {
extend: 'Ext.data.Model',
fields: [
{name: 'Alias", type:'string'},
{name:'Country',type:'string'}
]
});

constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'b_riCountries',
proxy: {type: 'ajax',
url: 'Htmls/countries.json',
reader: {
type: 'json',
root: 'countries'
}
},
model:'Country'
}, cfg)]);
}

vietits
14 Aug 2012, 5:29 AM
Make sure that store has loaded data from json file. This can be done by config your store with autoLoad: true or by calling <store>.load().