dijit.tree.Model --dojo学习

一个digit.Tree用来以图形化的方式表现一些层次结构的数据,对于实际的数据是使用TreeModel来表现。

一般来说,数据最终是来自一个data store,但是Tree提供的数据接口是“digit.tree.Model”,这是一个符合确定的API方法的对象,它可以使Tree能够访问各种格式的数据,比如items引用了它们的父Items的data store。

{name: 'folder1', type: 'directory'},
{name: 'file1', type: file, parent: 'folder1'}

 

这种方法要比一个父item有子item的列表格式要好:

{name: 'folder1', type: 'directory', children: ['file1']}

 

Tree有两种数据模型:
digit.tree.TreeStoreModel:这是一个data store的接口,它的特点是只能使用一个Item作为Tree的根。
digit.tree.ForestStoreModel:这是一个data store的接口,它的特点是可以使用多个Item作为Tree的顶级Item。

 

这两种模型有下列功能:
1、能够响应Tree widget的对Items和层次关系的查询
2、能够通知Tree当data store中下面的items发现了改变,可能是:
   新增了item,删除了item,改变了item(如修改了名称),item的孩子列表改变了等
3、控制Tree到data store的回写操作,通过DnD,DnD可以是tree内的items,或者外部挂接的Items

 

常用的API:(当你使用TreeStoreModel,ForestStoreModel时可能要覆盖的方法)

getChildren()

As documented above, getChildren() can work in various ways, depending on the structure of the data. Implementing a custom getChildren() method is what allows accessing data in the first example above, where children reference their parent rather than vice-versa.

mayHaveChildren()

For efficiency reasons, Tree doesn't want to query for the children of an item until it needs to display them. It doesn't want to query for children just to see if it should draw an exando (+) icon or not.

Thus, the method mayHaveChildren() returning true indicates that either:

  • the item has children
  • the item may have children but we'd have to query to find out

The default implementation of mayHaveChildren() checks for existence of the children attribute in the item (this assumes that parents point to their children rather than vice-versa), but it can and sometimes should be overridden to operate based on the type of item, for example:

return myStore.getValue(item, 'type') == 'folder';

pasteItem()

pasteItem() is called when something is dropped onto the Tree, and it's job is to update the data store. That sounds fairly simple, but it becomes complex when [you are using dijit.tree.ForestStoreModel and] the node being dropped will become a top level item in the data store.

For example, imagine that your data store contains all the countries in the world, and you are using dijit.tree.ForestStoreModel to fabricate a top-level Tree node called "World" that parents the countries. If the user drops a new country under "World", it needs to be added to the data store with some kind of flag indicating that it's a top-level node, and that code has to be custom written.

Similarly, if the data store has child elements point to their parents, rather than vice-versa, and the user reorders the children of a node, that ordering information needs to be persisted somehow to the data store.

onChildrenChange()

onChildrenChange() just notifies the tree about changes to a node's children, which is generally simple, but similar to above it needs to have special handling for top-level nodes in the data store. For example, if someone inserted a new country in the countries database listed above, the model would somehow need to realize that the data had changed, and notify the Tree that "world" had a new child.

 

转载于:https://www.cnblogs.com/mylem/archive/2009/11/25/1610095.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值