如何从dojo.data.ItemFileReadStore检索值 [英] How do I retrieve values from dojo.data.ItemFileReadStore
问题描述
首先,我在这里阅读了这个简短的帮助主题:点击
它使用与PHP构建的JSON文件,看起来像这样: / p>
{name:'Italy',键入:'country'},
{name:'North America',type :'大陆',
儿童:[{_参考:'墨西哥'},{_reference:'加拿大'},{_reference:'美利坚合众国}}},
{name:'墨西哥',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'人口:'400万',时区:' - 6 UTC'},
{名称:'加拿大',类型:'国家',人口:'33毫n',面积:'9,984,670平方公里',
儿童:[{_参考:'渥太华'},{_reference:'Toronto'}]},
所以我现在想回声这个列表中的所有城市...这对我来说没有问题! :-)
但是我完全不知道如何访问人口,例如!
我如何做一个函数echos:
墨西哥城市:人口:'19百万'时区:' - 6 UTC'例如?
你可以这样做:
var data = {items: [{name:'Mexico City',type:'city',population:'19 million',timezone:' - 6 UTC'}]};
var store = new dojo.data.ItemFileReadStore({data:data});
//或只要省略查询对象,如果你想要所有
store.fetch({query:{name:'Mexico City'},
onItem:function(item){
console.log(store.getValue(item,'name'));
console.log('population:',store.getValue(item,'population'));
控制台.log('timezone:',store.getValue(item,'timezone'));
}
});
请注意,您的数据应该有一个包含实际数据数组的项目键。 p>
诚然,dojo数据存储首先打包头很困难,但一旦你记得数据可以懒惰和异步地加载,这是很有道理的。这就是为什么所有的项目请求都通过 fetch
并且检索值通过 getValue
。
Dojocampus对于 ItemFileReadStore 有一个很好的说明。 p>
First, I read this short help thread here: CLICK
It uses a JSON file built together 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's say I now want to "echo" all the cities in this list ... that's no problem for me! :-) But I'm totally confused how to access the population for example! How can I make a function which echos: "Mexico City: population:'19 million' timezone:'-6 UTC'" for example?
You can do it like so:
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' ) );
}
});
Note, that your data should have an items key that holds an array of your actual data.
Admittedly, dojo data stores are a bit difficult to wrap your head around at first but it makes sense once you remember that the data could be loaded lazily and asychronously. That's why all requests for items go through fetch
and retrieving values goes through getValue
.
Dojocampus has a nice write-up about ItemFileReadStore.
这篇关于如何从dojo.data.ItemFileReadStore检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!