Search code examples
apache-flexarraysbindingflex3arraycollection

Updating runtime created arraycollections, they all have the same source


I have a source collection (now a simple Array). At run-time I create ArrayCollections using the same array as the source (each collection show the same source, but they are differently filtered). My problem is, when a new item added to the source, the already created arraycollections wont updated if one of the property of this new item is updated.

Anyone has a solution to this? What if my source is a Dictionary. How to create different ArrayCollections from the source dictionary, while the collections update whenever new item added, or an item is updated?

thanx


Solution

  • My solution is: Ive created a new class derived from ArrayCollection. Name it SourceCollection. Added a new private member that is a Dictionary, created with weakKeys turned to true. A new public function creates a new ArrayCollection from its elements, and add the reference of this created collection to the private dictionary like:

    public function createCollection():ArrayCollection
    {
       var result:ArrayCollection = new ArrayCollection();
           result.addAll(this);
       createdCollections[result] = null;
       return result;
    }
    

    Overrided the addItemAt, removeItemAt and removeAll function: each calls its super function and iterates through the dictionary, and do the appropriate function. Note addItem and addAll also calls addItemAt, so dont need to override them. One example is:

    override public function addItemAt(item:Object, index:int):void
    {
       super.addItemAt(item, index);
    
       for (var coll:Object in createdCollections)
       {
         (coll as ArrayCollection).addItemAt(item, index);
       }
    }
    

    Also added a test function that iterates through the dictionary, and count the items. If i dynamically creates Lists and assign ArrayCollection created from source with createCollection function, adding, removeing reflected fine, all has the same source items, that i wanted, and after removing dynamically created lists, after a while, tracked list count decreases automatically.

    If you put objects in the source that dispatches propertyChange event on any change, all Lists shows the change, too.