flex tree以及xml相关
sshong 发表于2010年8月8日 11:12:00 更新于2010年8月8日 11:16:00
1、当设置一个tree的dataprovider为某xml并且设置showroot=false时,如果一开始该xml没有任何子,则后续操作时,该tree总是会显示根节点!而如果该xml一开始有子节点或者等有子时在设置dataprovider,则不会出现此情况。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private var _times:int = 0;
            private var _xml:XML = <test label="root"></test>;
            private function onclick():void {
                var tmp:XML = <type label={_times}><item label="哈哈"/></type>;
                _xml.appendChild(tmp);
                _times ++;
                //等有子后设置,在这里就没有问题!!!!
//                if(_times == 1) {
//                    trace(_xml);
//                    myTree.dataProvider = _xml;
//                    myTree.showRoot = false;
//                }
            }
            private function init():void {
                //在这里设置就有问题,就是对于非showroot的tree,要等到有第一个子时设置才有效果
                myTree.dataProvider = _xml;
                myTree.showRoot = false;
            }
        ]]>
    </fx:Script>
    <mx:Tree x="30" y="26" id="myTree" width="247" height="365" labelField="@label"/>
    <s:Button x="402" y="26" label="appendxml" click="onclick()"/>
</s:Application>
2、众所周知,xml可以作为flex的dataprovider。当xml有更改时,会自动触发tree更新。但是,xml改变时是如何通知到tree的呢?xml转化为了XMLListCollection?但是奇怪的是,外部直接操作xml还是能影响tree显示,那么xml是如果通知到XMLListCollection的呢?

不查不知道,一查吓一跳。

在mx.utils下,有一个类XMLNotifier,这个XMLNotifier就是截取xml改变的关键所在。其提供了两个方法:watchXML、unwatchXML。仔细分析,发现,
watch中,设置xml改变时要触发的响应函数。
watcherFunction = initializeXMLForNotification();
xmlItem.setNotification(watcherFunction as Function);
unwatch中,获取xml改变时要触发的响应函数。
var xmlItem:XML = XML(xml);
var watcherFunction:Object = xmlItem.notification();
原来,xml中存在两个隐藏的方法,一个是setNotification,一个是notification,分别用于设置和获取xml改变时的响应。那么这个响应函数有什么要求吗?是的,这个函数的形参如下:
notificationFunction(currentTarget:Object,type:String,target:Object,value:Object,detail:Object):void
其中,type这个参数,对应的是xml的改变类型:
case "attributeAdded":
case "attributeChanged":
case "attributeRemoved":
case "nodeAdded":
case "nodeChanged":
case "nodeRemoved":
case "textSet":
case "namespaceAdded":
case "namespaceRemoved":
default:
具体各个参数的意义,可以参考这篇文章:
发现一个xml可以被侦听的方法 嘿嘿.
标签:xmltree分类:As3&Flex阅读:3421
评论
暂无评论
添加评论
您的大名,限长10汉字,20英文(*)
电子信箱(*)
您的网站
正文,限长500汉字,1000英文(*)
验证码(*) 单击刷新验证码
联系我
博客订阅