kumar z
kumar z

Reputation: 9

Flex: ArrayCollection should be read only

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

Answers (2)

drkstr101
drkstr101

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

CodeMonkey
CodeMonkey

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

Related Questions