1

I have several arraycollections (I don't know their number in advance) which contain one same object (among others).

var obj:MyObject = new MyObject();
var arc1:ArrayCollection = new ArrayCollection();
arc1.addItem(obj)
// same operation for my x arraycollections

Is it possible to delete my object "obj" in the first arraycollection and automatically delete it in all other arraycollections too without deleting it in each arraycollection one by one?

Filburt
  • 17,626
  • 12
  • 64
  • 115
tomy29
  • 11
  • 1

2 Answers2

0

Assuming that all your array collections share a common source, I would create ListCollectionViews instead of ArrayCollections and have them all point to a single ArrayCollection, i.e:

var masterCollection:ArrayCollection = new ArrayCollection();

for (var i:uint = 0; i < N; i++)
{
    slaveCollections[i] = new ListCollectionView(masterCollection);
}

Whenever you add or remove an item from any slaveCollection it will be added/removed from the master and all your other lists will be updated via the CollectionEvent.

dannrob
  • 1,061
  • 9
  • 10
0

Assuming that all your array collections do NOT share a common source, I would add a collection event listener to each collection to handle your requirement:

for (var i:uint = 0; i < N; i++)
{
    slaveCollections[i] = new ArrayCollection();
    slaveCollections[i].addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionListener);
}

...

private function collectionListener(event:CollectionEvent):void
{

   if (event.kind != CollectionEventKind.REMOVE)
        return

   for each(var slaveCollection:ArrayCollection in slaveCollections)
   {
      for each(var item:Object in event.items)
      {
          var itemIndex:int = slaveCollection.getItemIndex(item);
          if (itemIndex >= 0)
          {
              slaveCollection.removeItemAt(itemIndex);
          }
      }
   }

}

This should allow you to call: collection.removeItem(x) on any of your collections and have that item removed from the other ones.

dannrob
  • 1,061
  • 9
  • 10