Reputation: 9
Is there anyway to make ArrayCollection is readyonly.. So the client can only read the data..but additem or remove item
Example will be very helpfull
thanks in advance
Upvotes: 0
Views: 181
Reputation: 807
I usually just return a new instance from the source elements when I want to restrict access to an internal collection.
private var _ac:ArrayCollection;
public function get ac():ArrayCollection
{
return (_ac == null)? null : new ArrayCollection(_ac.toArray().concat());
}
Adding in the .contact() at the end ensures you get a new source instance rather than copying the existing one.
Upvotes: 1
Reputation: 174
This class will allow you to create an ArrayCollection with a source array using the new constructor, but you won't be able to add or remove items using the interface methods.
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;
}
}
}
Upvotes: 1