无论如何要使 ArrayCollection只是 .. 因此,客户只能读取数据..但additem或删除项目
示例将非常有帮助
提前致谢
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:1)
当我想限制对内部集合的访问时,我通常只从源元素返回一个新实例。
private var _ac:ArrayCollection;
public function get ac():ArrayCollection
{
return (_ac == null)? null : new ArrayCollection(_ac.toArray().concat());
}
在末尾添加.contact()可确保您获得新的源实例,而不是复制现有的实例。