`

Nested Items and Lazy Loading

阅读更多
Items can be handled in chunks, as well as streamed in. The other articles about datastores show items as a flat list with no hierarchy. So, what if you want a datastore to represent hierarchical data? And how do you walk across the hierarchy? Walking the hierarchy is, in fact, quite easy to do. The following example shows how to do this using JsonItemStore.

Assume a datasource of:


{ identifier: 'name',
     label: 'name',
     items: [
       { name:'Africa', type:'continent',
           children:[{_reference:'Egypt'}, {_reference:'Kenya'}, {_reference:'Sudan'}] },
       { name:'Egypt', type:'country' },
       { name:'Kenya', type:'country',
           children:[{_reference:'Nairobi'}, {_reference:'Mombasa'}] },
       { name:'Nairobi', type:'city' },
       { name:'Mombasa', type:'city' },
       { name:'Sudan', type:'country',
           children:{_reference:'Khartoum'} },
       { name:'Khartoum', type:'city' },
       { name:'Asia', type:'continent',
           children:[{_reference:'China'}, {_reference:'India'}, {_reference:'Russia'},          {_reference:'Mongolia'}] },
       { name:'China', type:'country' },
       { name:'India', type:'country' },
       { name:'Russia', type:'country' },
       { name:'Mongolia', type:'country' },
       { name:'Australia', type:'continent', population:'21 million',
           children:{_reference:'Commonwealth of Australia'}},
       { name:'Commonwealth of Australia', type:'country', population:'21 million'},
       { name:'Europe', type:'continent',
           children:[{_reference:'Germany'}, {_reference:'France'}, {_reference:'Spain'}, {_reference:'Italy'}] },
       { name:'Germany', type:'country' },
       { name:'France', type:'country' },
       { name:'Spain', type:'country' },
       { 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'}] },
       { name:'Ottawa', type:'city', population:'0.9 million', timezone:'-5 UTC'},
       { name:'Toronto', type:'city', population:'2.5 million', timezone:'-5 UTC' },
       { name:'United States of America', type:'country' },
       { name:'South America', type:'continent',
           children:[{_reference:'Brazil'}, {_reference:'Argentina'}] },
       { name:'Brazil', type:'country', population:'186 million' },
       { name:'Argentina', type:'country', population:'40 million' }
  ]}



The above datasource for JsonItemStore uses references to other items to build the hierarchy. Other datasources and datastores might use different internal representations for hierarchy. But, in this example, notice that the continent type items have children that are countries, which in turn have children that are cities.

The following code snippet walks across this hierarchy and displays all country items contained by the continent items:


var store = new dojo.data.ItemFileReadStore({url: "countries.json"});

//Load completed function for walking across the attributes and child items of the
//located items.
var gotContinents = function(items, request){
    //Cycle over all the matches.
    for(var i = 0; i < items.length; i++){
        var item = items[i];

        //Cycle over all the attributes.
        var attributes = store.getAttributes(item);
        for (var j = 0; j < attributes.length; j++){
            //Assume all attributes are multi-valued and loop over the values ...
            var values = store.getValues(item, attributes[j]);
            for(var k = 0; k < values.length; k++){
                var value = values[k];
                
                if(store.isItem(value)){
                    //Test to see if the items data is fully loaded or needs to be demand-loaded in (the item in question is just a stub).
                    if(store.isItemLoaded(value)){
                        console.log("Located a child item with label: [" + store.getLabel(value) + "]");
                    }else{
                        //Asynchronously load in the child item using the stub data to get the real data.
                        var lazyLoadComplete = function(item){
                            console.log("Lazy-Load of item complete.  Located child item with label: [" + store.getLabel(item) + "]");
                        }
                        store.loadItem({item: value, onItem: lazyLoadComplete});
                    }
                }else{
                    console.log("Attribute: [" + attributes[j] + "] has value: [" + value + "]");
                }
            }           
        }
    }
}

//Call the fetch of the toplevel continent items.
store.fetch({query: {type: "continent"}, onComplete: gotContinents});




RUN in IE6.0 RESULT:

Attribute: [name] has value: [Africa]
Attribute: [type] has value: [continent]
Located a child item with label: [Egypt]
Located a child item with label: [Kenya]
Located a child item with label: [Sudan]
Attribute: [name] has value: [Asia]
Attribute: [type] has value: [continent]
Located a child item with label: [China]
Located a child item with label: [India]
Located a child item with label: [Russia]
Located a child item with label: [Mongolia]
Attribute: [name] has value: [Australia]
Attribute: [type] has value: [continent]
Attribute: [population] has value: [21 million]
Located a child item with label: [Commonwealth of Australia]
Attribute: [name] has value: [Europe]
Attribute: [type] has value: [continent]
Located a child item with label: [Germany]
Located a child item with label: [France]
Located a child item with label: [Spain]
Located a child item with label: [Italy]
Attribute: [name] has value: [North America]
Attribute: [type] has value: [continent]
Located a child item with label: [Mexico]
Located a child item with label: [Canada]
Located a child item with label: [United States of America]
Attribute: [name] has value: [South America]
Attribute: [type] has value: [continent]
Located a child item with label: [Brazil]
Located a child item with label: [Argentina]
Tests:





Note: The previous sample also demonstrates how lazy-loading (on-demand) of items can be done through the combination of the isItemLoaded() and loadItem() functions. For a demonstration of a lazy-loading approach that uses an extended version of ItemFileReadStore, see the demo in dojox/data/demos/demo_LazyLoad.html.

分享到:
评论

相关推荐

    Nested list and file.txt

    You're going to write a program that lets the user enter a sentence and translates it to another language. The program will learn. When it first comes across a word it doesn't have in its dictionary, ...

    Spring Nested事务简单案例

    在本案例中,我们关注的是Spring中的Nested事务,这是一个相对复杂的特性,但非常有用,特别是在需要子事务处理的场景下。下面将详细解释这个概念。 Nested事务是基于JDBC的Savepoint机制实现的,它可以让我们在一...

    oracle nested table demo

    它支持多种高级数据结构,其中就包括嵌套表(Nested Table)。嵌套表是一种特殊类型的集合类型,允许在一个列中存储一组行,这些行可以是同一种类型的数据。本示例“Oracle Nested Table Demo”将向我们展示如何在...

    code for Holistically-Nested Edge Detection

    holistically-nested edge detection (HED), which performs image-to-image prediction by means of a deep learning model that leverages fully convolutional neural networks and deeply-supervised nets....

    Weblogic9异常解决nested errors.txt

    标题和描述均提到了“Weblogic9异常解决nested errors”,这指向了在使用WebLogic Server 9版本时可能遇到的特定错误处理问题。WebLogic Server是Oracle公司提供的一款功能强大的应用服务器,它支持多种标准协议,如...

    acts_as_nested_set的增强版BetterNestedSet

    《深入理解BetterNestedSet:acts_as_nested_set的增强版》 在Ruby on Rails开发中,数据结构的选择对于数据库操作的效率至关重要。其中,Nested Set模型是一种常用的数据组织方式,尤其适用于管理具有层级关系的...

    当设置lazy=true时,怎样使用extjs和Hibernate

    当我们设置Hibernate中的实体属性`lazy=true`时,这是懒加载(Lazy Loading)机制的体现。懒加载是一种优化策略,它允许我们在需要时才加载关联的对象,而不是在初始加载实体时就加载所有关联数据,从而提高性能。 ...

    Laravel开发-laravel-nestedset

    这就是Laravel Nestedset派上用场的地方。Nestedset是一种高效的存储和操作具有层级关系的数据模式,它允许我们进行快速的查询和便捷的操作,如插入、删除和移动节点。 Laravel Nestedset库,如`laravel-nestedset`...

    Wavelets and Filter Banks_MIT

    Nested Spaces and Complementary Spaces Scaling Functions and Wavelets Lecture 10 Refinement Equation: Iterative and Recursive Solution Techniques Infinite Product Formula Filter Bank Approach for ...

    Laravel开发-laravel-nested

    "laravel-nested"很可能是一个用于在Laravel中实现嵌套集合(Nested Set)模型的库或者教程。嵌套集合是一种常用的数据结构,特别适合表示具有层级关系的数据,比如目录结构、组织架构等。 在Laravel中,我们通常...

    Improving Nested Loop Pipelining on Coarse-Grained Reconfigurable Architectures

    and efficiency. Loops in applications are often mapped onto CGRAs for acceleration, and the mapping of loops onto CGRA is quite a challenging work due to the parallel execution paradigm and ...

    json--nested-sample

    // getters and setters } public class Address { private String street; private String city; // getters and setters } ``` 要将`Person`对象转换为JSON字符串,可以使用`Gson.toJson()`方法: ...

    基于Nested Logit模型的出行路线方式选择和时间价值计算

    基于Nested Logit模型的出行路线方式选择和时间价值计算,宗芳,祁文田,本文介绍了Nested Logit模型的效用最大化理论、选择树的建立以及其概率表达式;时间价值的基本计算公式。对人们由长春到吉林的出行路

    The Nested Model 树形结构

    "Nested Model"(嵌套模型)是处理层级数据的一种高效且灵活的方法,通常用于表示具有父子关系的数据,比如组织架构、分类目录等。在本篇博文中,我们将探讨这种模型以及如何在实际应用中利用它。 嵌套模型的核心...

    react-native-nested-scroll-view,Android NestedScrollView的React本机包装.zip

    在React Native开发中,我们经常会遇到需要在一个组件中嵌套多个可滚动视图的情况,这时`react-native-nested-scroll-view`就派上用场了。它是一个针对Android平台的NestedScrollView的React Native封装,旨在解决...

    Laravel开发-eloquent-nested-attributes

    Laravel开发-eloquent-nested-attributes 嵌套属性允许您通过父级保存关联记录的属性。默认情况下,嵌套属性更新被关闭,您可以使用$nested属性启用它。启用嵌套属性时,将在模型上定义属性编写器。

    sqlitejdbc-v037-nested

    sqlitejdbc-v037-nested.jar

    asm5.0.jar

    Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.:java.lang.IncompatibleClassChangeError: class ...

    java解决nested exception is java.lang.OutOfMemoryError Java heap space

    Java程序在运行过程中可能会遇到各种异常,其中"nested exception is java.lang.OutOfMemoryError: Java heap space"是一个常见的问题,通常发生在程序试图分配超过堆内存限制的空间时。这个错误表明Java虚拟机(JVM...

Global site tag (gtag.js) - Google Analytics