首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

提高 Dojo Grid 的数据处理性能(2)Dojo Grid 的数据存储

提高 Dojo Grid 的数据处理性能(2)Dojo Grid 的数据存储

Dojo Grid 的数据存储      在 Grid Model 的定义中,有一个叫 Store 的属性,它存储了与 Grid 相关联的数据,也就是 Grid 绑定到的数据源。      在示例中,数据源的名字叫 modelStore。modelStore 的定义如下:<div dojotype="dojo.data.ItemFileWriteStore"       jsid="modelStore" url="data/modelItemList.json"></div>
     Dojo 的核心提供了一个只读的数据体实现,ItemFileReadStore。这个数据体可以从 HTTP 端点读取 Json 结构体,或是读取内存中的 JavaScript 对象。     Dojo 允许为 ItemFileReadStore 指定某个属性来作为 identifier(唯一标识符)。Dojo 内核同时提供了 ItemFileWriteStore 存储作为 ItemFileReadStore      的一个扩展,它是建立在 dojo.data.api.Write 和 dojo.data.api.Notification API 对 ItemFileReadStore 的支持之上的。如果你的应用程序需要     写入数据到 ItemFileStore,则 ItemFileWriteStore 正是你所要的。
           对于 Store 的操作,可以使用函数 newItem, deleteItem, setValue 来修改 Store 的内容,这些操作可以通过调用 revert 函数来取消,           或者调用 save 函数来提交修改。
           在使用这些函数时,一定要注意的是,如果你为 ItemFileWriteStore 指定了某个属性来作为 identifier,必须要确保它的值是唯一的,           这对 newItem 和 deleteItem 函数特别重要,ItemFileWriteStore 使用这些 ID 来跟踪这些改变,这意味着即使你删除了一个 Item,           它的 identity 还是被保持为使用状态,因此,如果你调用 newItem() 并试图重用这个 identifier,你将得到一个异常。要想重用这个 identifier,           你需要通过调用 save() 来提交你的改变。Save 将应用所有当前的改变并清除挂起的状态,包括保留的 identifier。当你没有为 Store 指定 identifier 时,           则不会出现上述问题。原因是,Store 会为每个 Item 自动创建 identifier,并确保了它的值是唯一的。在这种自动创建 identifier 的情况下,           identifier 是不会作为一个公有属性暴露出来的(它也不可能通过 getValue 得到它,仅 getIdentity 可以)。
     Store 的数据存储在两个 json 数组中,名字分别为 _arrayOfAllItems 和 _arrayOfTopLevelItems。这两个数组的区别是在于前者记录了 Grid 创建以来 Store 中存在过的所有变量,           后者中只是存储 Store 当前的所有 Item。如果有变量被删除,则 _arrayOfAllItems 中该数组变量设为 null,而 _arrayOfTopLevelItems 中该数组变量会被彻底删除,   数组个数减一。这样的设定是为了在 Store.newItem 的时候,如果用户没有为 Store 指定 identifier,Store 可以自动地用 _arrayOfAllItems 的数量值为新 Item    创建 identifier。_arrayOfAllItems 的个数不会因为删除操作而减少,也就不必担心新 Item 的 identifier 就会发生重复。
   程序清单 3 中描述了 Grid 自动创建 identifier 的过程,首先程序会尝试去获得之前定义的 Identifier 属性。如果属性是 Number 就把 _arrayOfAllItems.length 赋给 newIdentity。如果属性不是 Number,   就把 identifierAttribute 对应的 keywordArgs 赋给 newIdentity,如果 newIdentity 是空,就表示 identity 创建失败。
清单 3. Grid identifier 的创建
1
2
3
4
5
6
7
8
9
10
11
var newIdentity = null;
var identifierAttribute = this._getIdentifierAttribute();
if(identifierAttribute === Number){
newIdentity = this._arrayOfAllItems.length;
}
else{
newIdentity = keywordArgs[identifierAttribute];
if (typeof newIdentity === "undefined"){
throw new Error("newItem() was not passed an identity for the new Item");
}
}




  图 2 则显示了当删除一个数据后,_arrayOfAllItems 和 _arrayOfTopLevelItems 的差别。_arrayOfAllItems 有个变量被置空,而 _arrayOfTopLevelItems 的个数减一了。   
图 2. arrayOfAllItems 和 arrayOfTopLevelItems
返回列表