0

有没有办法让 ArrayCollection is readyonly .. 所以客户端只能读取数据..但是 additem 或 remove item

示例将非常有帮助

提前致谢

4

2 回答 2

1

当我想限制对内部集合的访问时,我通常只从源元素返回一个新实例。

private var _ac:ArrayCollection;

public function get ac():ArrayCollection
{
  return (_ac == null)? null : new ArrayCollection(_ac.toArray().concat());
}

最后添加 .contact() 可确保您获得一个新的源实例,而不是复制现有的实例。

于 2013-09-10T23:26:48.220 回答
1

此类将允许您使用新的构造函数创建带有源数组的 ArrayCollection,但您将无法使用接口方法添加或删除项目。

package
{
import mx.collections.ArrayCollection;
import mx.collections.IList;

public class ArrayCollectionReadOnly extends ArrayCollection
{
    public function ArrayCollectionReadOnly(source:Array=null)
    {
        super(source);
    }

    override public function addAll(addList:IList):void {
        throw new Error("Illegal Operation, read only");    
    }

    override public function addAllAt(addList:IList, index:int):void {
        throw new Error("Illegal Operation, read only");    
    }

    override public function addItem(item:Object):void{
        throw new Error("Illegal Operation, read only");    
    }

    override public function addItemAt(item:Object, index:int):void{
        throw new Error("Illegal Operation, read only");    
    }

    override public function removeAll():void {
        throw new Error("Illegal Operation, read only");    
    }

    override public function removeItemAt(index:int):Object {
        throw new Error("Illegal Operation, read only");
        return null;
    }

    override public function setItemAt(item:Object, index:int):Object{
        throw new Error("Illegal Operation, read only");
        return null;
    }
}

}
于 2013-09-10T18:55:26.390 回答