How to get values ​​from dojo.data.ItemFileReadStore

First, I read this short help: CLICK

It uses a JSON file created with PHP, which looks something like this:

{ name:'Italy', type:'country' }, { name:'North America', type:'continent', children:[{_reference:'Mexico'}, {_reference:'Canada'}, {_reference:'United States of America'}] }, { name:'Mexico', type:'country', population:'108 million', area:'1,972,550 sq km', children:[{_reference:'Mexico City'}, {_reference:'Guadalajara'}] }, { name:'Mexico City', type:'city', population:'19 million', timezone:'-6 UTC'}, { name:'Guadalajara', type:'city', population:'4 million', timezone:'-6 UTC' }, { name:'Canada', type:'country', population:'33 million', area:'9,984,670 sq km', children:[{_reference:'Ottawa'}, {_reference:'Toronto'}] }, 

So, let me say that now I want to "echo" all the cities on this list ... there is no problem for me! :-) But I'm completely confused about how to get access to the population, for example! How can I make a function that echoes: "Mexico City: population:" 19 million "time zone:" 6 UTC "" for example?

+6
json javascript dojo
source share
1 answer

You can do it like this:

 var data = { items: [{ name:'Mexico City', type:'city', population:'19 million', timezone:'-6 UTC'}]}; var store = new dojo.data.ItemFileReadStore( { data: data }); // or just omit query object if you want all store.fetch( { query: { name: 'Mexico City' }, onItem: function(item) { console.log( store.getValue( item, 'name' ) ); console.log( 'population: ', store.getValue( item, 'population' ) ); console.log( 'timezone: ', store.getValue( item, 'timezone' ) ); } }); 

Please note that your data must have an element key that contains an array of your actual data.

Admittedly, dojo datastores are a little harder to wrap your head around, but that makes sense as soon as you remember that data can be loaded lazily and asynchronously. Therefore, all requests for elements go through fetch , and the resulting values ​​go through getValue .

Dojocampus has a good review of ItemFileReadStore .

+10
source share

All Articles