Dojo学习15 dojo.data Read API

15. dojo.data Read API
Dojo.data 中最基础的API(或者说是接口)就是Read API. 所有的Store都必须实现这个接口,因为所有的store都需要获取和处理item(数据项)。针对处理数据项,Read API被设计的非常灵活。Read API提供如下的功能:
?         查看datastore都实现了那些接口,具有什么功能。通过getFeatures()方法。
?         查看一个item所包含的所有属性而不需要了解这个item的格式。getAttributes(item)方法
?         比如:一个item记录着一个学生的信息。通过getAttribuets()方法,你能得到这个学生包括哪些信息,比如:姓名,学号,等等。
?         获取item的属性的值,而不用知道item的格式。
?         Item的保存格式有很多中,有json或者csv或者数据库中保存。通过getValue方法,你就能得到一个item的属性的值,转换数据格式交给store来做。
?         查看所有的items的属性,看看有没有指定的值。
?         验证一个js Object是不是store产生的item.
?         查看一个item是不是被完全加载了,还是仅仅是一个需要被加载的根(stub). isItemLoaded()
?         加载一个stub item ,这个方法就是懒加载的时候使用的。loadItem()
?         通过一些查询条件来查询items
?         给一些items排序
?         为一个查询结果分页。(我觉得对非数据库型的查询也许会有用一些,至少能使操作方便一些吧)
?         通过通配符( * 和 ? )来过滤查询结果
一些例子:
1、  查看一个store支持那些APIs
var store = new some.Datastore();
var features = store.getFeatures();
for(var i in features){
    console.log("Store supports feature: " + i);
}
2、  测试一个对象是不是一个item
var store = new some.Datastore();
if(store.isItem(someObject)){
    console.log("Object was an item.");
}else{
    console.log("Object was NOT an item.");
}
3、  列举一个item的所有属性
var store = new some.Datastore();
...
//Assume that someItem is an item we got from a load.
var attributes = store.getAttributes(someItem);
for(var i = 0; i < attributes.length; i++){
    console.log("Item has attribute; " + attributes[i]);
}
4、  测试一个item是否包含特定的属性
var store = new some.Datastore();
...
//Assume that someItem is an item we got from a load.
var attributes = store.getAttributes(someItem);
for(var i = 0; i < attributes.length; i++){
    console.log("Item has attribute; " + attributes[i]);
}
5、  得到一个item的label
 var store = new some.Datastore();
...
//Assume that someItem is an item we got from a load.
var label = store.getLabel(someItem);
console.log("item has label: " + label);

//其实一个item的label也是一个item的属性,只不过是一个特殊的属性。在json的开始部分:label:”name” 这个就声明了,item的name属性就是item的label,所以通过getLabel就得到了name属性,也就等同于getValue(item,”name”);
6、得到一个store的所有数据
var pantryStore = new dojo.data.JsonItemStore({url: "pantry_items.json" } );

//Define the onComplete callback to write COMPLETED to the page when the fetch has finished returning items.
var done = function(items, request){
    document.appendChild(document.createTextNode("COMPLETED"));
}

//Define the callback that appends a textnode into the document each time an item is returned.
gotItem = function(item, request) {
    document.appendChild(document.createTextNode(pantryStore.getValue(item, "name"));
    document.appendChild(document.createElement("br"));
}

//Define a simple error handler.
var gotError = function(error, request){
    alert("The request to the store failed. " +  error);
}

//Invoke the search
pantryStore.fetch({onComplete: done, onItem: gotItem, onError: gotError}); 
//这里onComplete和onItem同时使用的时候onComplete的items参数是null,这点请注意。

7、  通过一个标识得到一个item
var pantryStore = new dojo.data.JsonItemStore({url: "pantry_items.json" } );
   var pepperItem = pantryStore.getItemByIdentity("pepper");
   if (pepperItem !== null){
        alert('Pepper is in aisle ' + pantryStore.getValue(pepperItem,"aisle");  
   }
 //这个标识在json的开头部分有声明, identifier: 'name' ,也就是说item的name属性是标识。

8、通过条件获取数据
jsonItemStore.fetch({
        queryOptions: {ignoreCase: true},  //忽略大小写
        query: { name: "*pepper*", aisle: "Spices" }, //name 和 aisle是属性的名称
        onComplete:
                ...  
   });
9、 嵌套定义item
一个item可以包括多个子item就像一棵树一样。使用reference可以关联子节点。通过getValues()方法可以得到孩子节点的数组。
10、 分页
    var store = new dojo.data.JsonItemStore({url: "pantryStore.json" });
    var pageSize = 10;
    var request = null;
    var outOfItems = false;

    var onNext = function(){
       if(!outOfItems){
           request.start += pageSize;
           store.fetch(request);
       }
    };

    var onPrevious = function(){
       if (request.start > 0){
          request.start -= pageSize;
          store.fetch(request);
       }
    }
    var itemsLoaded = function(items, request){
       if (items.length < pageSize){
       
          outOfItems = true;
       }else{
          outOfItems = false;
       }
       ...
    }

request = store.fetch({onComplete: itemsLoaded, start: 0; count: pageSize});

11、排序
var store = new dojo.data.JsonItemStore({url: "pantryStore.json"});

var sortKeys = [
    {attribute: "aisle", descending: true},
    {attribute: "name", descending: false}
];

store.fetch({
       sort: sortKeys;
       onComplete:
           ...
});
//When onComplete is called, the array of items passed into it
//should be sorted according to the denoted sort array.

你可能感兴趣的:(Dojo学习15 dojo.data Read API)