【问题标题】:How do I retrieve values from dojo.data.ItemFileReadStore如何从 dojo.data.ItemFileReadStore 检索值
【发布时间】:2009-09-02 12:26:00
【问题描述】:

首先,我在这里阅读了这个简短的帮助帖子:CLICK

它使用一个与 PHP 一起构建的 JSON 文件,看起来像这样:

{
  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'
  }]
},

假设我现在想“回显”这个列表中的所有城市......这对我来说没问题! :-) 但是,例如,我对如何访问人口完全感到困惑! 如何制作一个回显的函数: “墨西哥城:人口:'1900万'时区:'-6 UTC'”例如?

【问题讨论】:

    标签: javascript json dojo


    【解决方案1】:

    你可以这样做:

    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' ) );
                   }
    });
    

    请注意,您的数据应该有一个包含实际数据数组的 items 键。

    诚然,dojo 数据存储一开始有点难以理解,但一旦您记住数据可以延迟和异步加载,它就会变得有意义。这就是为什么对项目的所有请求都通过fetch 而检索值通过getValue

    Dojocampus 有一篇关于ItemFileReadStore 的精彩文章。

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 2018-04-14
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多