谁动了我的ArrayCollection?What Happened to My ArrayCollection

突然发现网站名字改了。 RIA 上海。 从地域性的限制来看,网站越来越不适合我了 :p

今天程序 Debug, 发现了一个针对ArrayCollection时,要比较细心处理的地方。防止ArrayCollection中的元素无缘无故的消失掉。

当应用ArrayCollection的filterFunction属性时,AC的长度变化要细心处理。明确自己是要应用被过滤前,还是过滤后的ArrayCollection的长度。

下面的代码测试了各个状态下,AC的长度。

 

        private var ac:ArrayCollection;

        private function onInit():void
        {
            ac = new ArrayCollection([1,2,3,4,5,6,7]);
            trace("The ArrayCollection length is: " + ac.length);
            ac.filterFunction = filterFun;
            trace("The ArrayCollection length before refresh: " + ac.length);
            ac.refresh();
            trace("The ArrayCollection length after refresh: " + ac.length);
            trace("The source lenght of ArrayCollection: " + ac.source.length);
        }

        private function filterFun(item:int):Boolean
        {
            if(item > 5)
                return true;
            else
                return false;
        }

这里用filterFunction过滤掉小于等于5的数字。当AC用完过滤方程后,只剩长度2, 6和7将被留下。输出结果如下,

The ArrayCollection length is: 7
The ArrayCollection length before refresh: 7
The ArrayCollection length after refresh: 2    --- 过滤并且马上使用refresh()方法后,长度变成2
The source lenght of ArrayCollection: 7  
--- 但是起source array的长度还是原来的长度7,我们可以看到用过滤方程,不是真的把数字从AC中删除, 仅仅是遮盖住罢了。如果我们用RemoteObject把这个过滤过的AC发送给JAVA端处理的话,其操作的AC长度还是7。