dojo.data.ItemFileReadStore--dojo学习

dojo的内核提供了一种对只读数据存储类型的解决方案:ItemFileReadStore,这种store可以从url、

webService或者内存中的javascript对象读取json结构的数据,并将所有Item存在内存中供快速的访问。
ItemFileReadStore被设计用来灵活的表达item的层次关系,引用关系和自定义的数据类型,它也提供了一些属

性如唯一标识ID,item的文本名称等,

提示:
1、ItemFileReadStore所使用的格式不需要使用dojo.data.API.该格式可以在中等数据量的环境下,来表示

javascript对象Tree.它并不是一种大多数数据存储使用的一种很标准权威的数据格式,比如有些数据存储使用

CSV数据结构,XML,Opml,Html,raw文本等等。一个数据存储最重要的是要隐藏内部结构,但通过标准的API向

外部提供数据。
2、这只是一种内存中的数据存储,而不是设计来为远程数据服务的操作之用。

支持的APIs

ItemFileReadStore实现了下列dojo.data APIs
 dojo.data.api.Read
 dojo.data.api.Identity

构造函数参数:
url:这个可选参数指出要读入json数据的URL,注意这个URL仅读入一次,因为它只是一个内存中的数据存储,

仅当和参数clearOnClose和urlPreventCache一同使用进,它才可能会重新调入新的数据。
data: 用来填充Store的一个结构化的javascript对象,这个参数可以代替URL以用来读取不同的数据,如果设定了url,则此参数被忽略,传入的数据对象将会直接使用并可能被修改。
typeMap:这是一个javascript 对象,用来定义如何序列化自定义类型
clearOnClose:这是一个bool值的参数,用来控制close()函数的行为,如果设为true,则调用close()会清除store中内部结构中的所有数据,并从url获取新的数据集,或者从data属性中获取新的数据对象。1.4版以前仅支持url属性的更新。
urlPreventCache:这个bool值的参数,用来控制dojo是否干预浏览器的缓存,当使用clearOnClose来Reload数据时,非常有用。
hierarchical:这个bool值 的参数,控制ItemFileReadStore是否认为数据是有层次结构的,默认值为true

输入数据格式:

ItemFileReadStore希望输入数据格式为以下格式:

 

General Structure
{
  "label": "some attribute",   //Optional attribute used to indicate which attribute on an item should act as a human-readable label for display purposes.


  "identifier": "some attribute",  //Optional attribute used to indicate which attribute on an item acts as a unique identifier for that item. If it is not defined, then the ItemFileReadStore will simply number the items and use that number as a unique index to the item.


  "items:" [  //The array of JavaScript objects that act as the root items of the data store
    { /* Some set of name/value attributes */ },
    { /* ... */ },
    ...
  ]
}

结点结构:

对于ItemFileReadStore来说,任何JavaScript对象都被认为是一个Item,包括一个Js对象的属性的一个值,通

常情况下,string,boolean, integer, object等都能作为属性值,还有一些特殊结构与常规变量类型不同。

首先是_reference结构,这个结构是一个javascript对象,有着_reference的特定属性。它的值只能是另一个Item的ID,对于这种结构,ItemFileReadStore能够读入并配置Items之间的关系,下面是一个例子:

 

{
  "identifier": "name",
  "items": [
    { "name": "Bob", "spouse": {"_reference":"Sally"),  "children": [ { "_reference":"Nancy"}]},
    { "name": "Sally", "spouse": {"_reference":"Bob"),  "children": [ { "_reference":"Nancy"}]},
    { "name": "Nancy"},
  ]
}

所以,通过调用store.getValue(bobItem,"spouse"),将返回数据存储Item的ID:Sally.

 

自定义结构类型

其他特殊结构就是自定义结构类型

{
  "items": [
    { "name": "Bob", "birthdate": { "_type": "Date", "_value": "1965-08-20T00:00:00Z"})
  ]
}

当ItemFileReadStore解析这个结构时,它探测到JavaScript对象的属性值:birthdate有特殊的属性"_type"和

"_value",它会去搜索存储的类型图(type map of the store),并测定"_type"的值是否已经定义了,如果

是,则它取出"_value",并根据数据类型返回一个Date对象。

 

 

Item Structure Examples

Items with References
{
  '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' }
  ]
}
Items with Hierarchy
{
  'identifier': 'name',
  'label': 'name',
  'items': [
    { 'name':'Africa', 'type':'continent', 'children':[
      { 'name':'Egypt', 'type':'country' },
      { 'name':'Kenya', 'type':'country', 'children':[
        { 'name':'Nairobi', 'type':'city' },
        { 'name':'Mombasa', 'type':'city' } ]
      },
      { 'name':'Sudan', 'type':'country', 'children':
        { 'name':'Khartoum', 'type':'city' }
      } ]
    },
    { 'name':'Asia', 'type':'continent', 'children':[
      { 'name':'China', 'type':'country' },
      { 'name':'India', 'type':'country' },
      { 'name':'Russia', 'type':'country' },
      { 'name':'Mongolia', 'type':'country' } ]
    },
    { 'name':'Australia', 'type':'continent', 'population':'21 million', 'children':
      { 'name':'Commonwealth of Australia', 'type':'country', 'population':'21 million'}
    },
    { 'name':'Europe', 'type':'continent', 'children':[
      { 'name':'Germany', 'type':'country' },
      { 'name':'France', 'type':'country' },
      { 'name':'Spain', 'type':'country' },
      { 'name':'Italy', 'type':'country' } ]
    },
    { 'name':'North America', 'type':'continent', 'children':[
      { 'name':'Mexico', 'type':'country',  'population':'108 million', 'area':'1,972,550 sq km', 'children':[
        { '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':[
        { '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':[
      { 'name':'Brazil', 'type':'country', 'population':'186 million' },
      { 'name':'Argentina', 'type':'country', 'population':'40 million' } ]
    }
  ]
}
Items with Custom Types
{
  'identifier': 'abbr',
  'label': 'name',
  'items': [
    { 'abbr':'ec', 'name':'Ecuador',           'capital':'Quito' },
    { 'abbr':'eg', 'name':'Egypt',             'capital':'Cairo' },
    { 'abbr':'sv', 'name':'El Salvador',       'capital':'San Salvador' },
    { 'abbr':'gq', 'name':'Equatorial Guinea', 'capital':'Malabo' },
    { 'abbr':'er',
      'name':'Eritrea',
      'capital':'Asmara',
      'independence':{'_type':'Date', '_value':"1993-05-24T00:00:00Z"}
    },
    { 'abbr':'ee',
      'name':'Estonia',
      'capital':'Tallinn',
      'independence':{'_type':'Date', '_value':"1991-08-20T00:00:00Z"}
    },
    { 'abbr':'et',
      'name':'Ethiopia',
      'capital':'Addis Ababa' }
  ]
}
General Case Type Map
这种类型图适用于在它被转换成对象格式之前,必须对属性值进行一些处理的情况,一般用于一个单独的值不能直接用作构造函数的参数的情况。
 
    

General Form Type Map

{
  "type0": {
        "type": constructorFunction(),
        "deserialize": function(value)
  },
  "type1": {
        "type": constructorFunction(),
        "deserialize": function(value)
  },
  "type2": {
        "type": constructorFunction(),
        "deserialize": function(value)
  },
  ...
  "typeN": {
    "type": constructorFunction(),
    "deserialize": function(value)
  }
}
Example: General Case Type Map for JavaScript Date Objects
{
  "Date": {
    type: Date,
    deserialize: function(value){
      return dojo.date.stamp.fromISOString(value);
    }
  }
}

Simplified Form Type Map

这种类型图在使用时更加简单,它可以直接作为参数传递给构造函数使用。

 

Simplified Form Type Map

{
  "type0": constructorFunction(),
  "type1": constructorFunction(),
  "type2": constructorFunction(),
  ...
  "typeN": constructorFunction()
}

 自定排序

 

ItemFileReadStore 使用 dojo.data.util.sorter helper 函数来实施item 的排序. 这些函数提供了一个解决方案,使用最终用户可以定制根据哪些属性来排序. 通过大store class上定义一个 comparatorMap.The comparator map 映射一个属性名到一些排序函数. 排序函数会返回 1, -1, or 0, 根据两个item的属性比较是大于,小于, 或是等于. 下面是一个对属性foo的自定排序的例子:

var store = new dojo.data.ItemFileReadStore({data: { identifier: "uniqueId",
  items: [ {uniqueId: 1, status:"CLOSED"},
    {uniqueId: 2,  status:"OPEN"},
        {uniqueId: 3,  status:"PENDING"},
        {uniqueId: 4,  status:"BLOCKED"},
        {uniqueId: 5,  status:"CLOSED"},
        {uniqueId: 6,  status:"OPEN"},
        {uniqueId: 7,  status:"PENDING"},
        {uniqueId: 8,  status:"PENDING"},
        {uniqueId: 10, status:"BLOCKED"},
        {uniqueId: 12, status:"BLOCKED"},
        {uniqueId: 11, status:"OPEN"},
        {uniqueId: 9,  status:"CLOSED"}
      ]
}});

//Define the comparator function for status.
store.comparatorMap = {};
store.comparatorMap["status"] = function(a,b) {
  var ret = 0;
  // We want to map these by what the priority of these items are, not by alphabetical.
  // So, custom comparator.
  var enumMap = { OPEN: 3, BLOCKED: 2, PENDING: 1, CLOSED: 0};
  if (enumMap[a] > enumMap[b]) {
    ret = 1;
  }
  if (enumMap[a] < enumMap[b]) {
    ret = -1;
  }
  return ret;
};

var sortAttributes = [{attribute: "status", descending: true}, { attribute: "uniqueId", descending: true}];
function completed(items, findResult){
  for(var i = 0; i < items.length; i++){
    var value = store.getValue(items[i], "uniqueId");
    console.log("Item ID: [" + store.getValue(items[i], "uniqueId") + "] with status: [" + store.getValue(items[i], "status") + "]");
  }
}
function error(errData, request){
  console.log("Failed in sorting data.");
}

//Invoke the fetch.
store.fetch({onComplete: completed, onError: error, sort: sortAttributes});

Query Syntax查询语法

ItemFileReadStore的 fetch 方法的查询语法是很简单和直接的. 它允许一个属性列表 to match against in an AND fashion. 例如:一个query对象找出所有 items 中具有foo属性= bar 并且 bit属性=bite,就是下面这样:

{ foo:"bar", bit:"bite"}

OK,非常容易吧,而且 ItemFileReadStore还支持通配符查询:多字符: * 和单字符 ?

NOTE: As of The dojo Toolkit 1.4, a RegularExpression object can also be passed as a query on an attribute. This is not dojo.data.api specified, but a feature specific to ItemFileReadStore. Do not expect other stores to implement it. Some may, some may not. Always refer to their documentation on their query syntax.

{ foo:new RegExp("/^bar$/"), bit:/^bite$/}

 

Query Options

Dojo.data defines support for a 'queryOptions' modifier object that affects the behavior of the query. The two defined options listed by the API are ignoreCase and deep. ItemFileReadStore supports these options. The affect of these options on a query is defined below.

OptionResult
ignoreCaseThe default is false. When set to true, the match on attributes is done in a case-insensitive fashion. This means with ignoreCase: true, a query of A* would match Apple and acorn
deepThis option affects searching when the structure passed to ItemFileReadStore has hierarchy. For an example of that, refer to Item Structure Example: Items with Hierarchy. the default value for this option is false, which means the query is only applied against root items in the tree of data items. If it is set to true, then the query is applied to root items and all child data items. Think of it as a recursive search.

 

参考:

 http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html

 

 

 

 

转载于:https://www.cnblogs.com/mylem/archive/2009/11/16/1603749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值