This is possible if you use the class below. It extends ArrayCollection and
adds two methods:
addReflection(reflection:ArrayCollection)
removeReflection(reflection:ArrayCollection)
This class makes sure the reflections share the list (inner dataProvider) of
the main collection. If something changes in the list all child components
will be refreshed automatically.
This class ensures that more collections share the same data, this helps you
in the sence that you dont need to copy the collections if you want to use
different filters for different views.
Greetz Erik
package fly.flex.collections
{
import mx.collections.ArrayCollection;
import flash.utils.Dictionary;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;
import mx.collections.IList;
import mx.binding.utils.BindingUtils;
import mx.binding.utils.ChangeWatcher;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class ReflectorArrayCollection extends ArrayCollection
{
private var _reflections:Dictionary;
private var _timer:Timer;
private var _reflectionsNeedRefresh_bool:Boolean;
public function ReflectorArrayCollection(source:Array = null)
{
super(source);
_reflections = new Dictionary(true);
_timer = new Timer(0);
_timer.addEventListener(TimerEvent.TIMER, _timerHandler);
};
private function _timerHandler(e:TimerEvent):void
{
if (_reflectionsNeedRefresh_bool)
{
_reflectionsNeedRefresh_bool = false;
var obj:Object;
var reflection:ArrayCollection;
for (obj in _reflections)
{
reflection = obj as ArrayCollection;
reflection.refresh();
};
};
};
private function _collectionChangeHandler(e:CollectionEvent):void
{
/*
If we would refresh the reflections here they items would not yet
be available. Apparently this handler is called too fast. A handler
outside of this class would work. To solve it we use a timer.
*/
_reflectionsNeedRefresh_bool = true;
_timer.start();
};
public function addReflection(reflection:ArrayCollection):void
{
_reflections[reflection] = BindingUtils.bindProperty(reflection, list,
this, list);
};
public function removeReflection(reflection:ArrayCollection):void
{
var watcher:ChangeWatcher = _reflections[reflection];
watcher.unwatch();
delete _reflections[reflection];
};
/*
The list setter is overridden in order to make sure we allways have a
listener
to the correct internal list.
*/
override public function set list(value:IList):void
{
if (list)
{
list.removeEventListener(CollectionEvent.COLLECTION_CHANGE,
_collectionChangeHandler);
};
super.list = value;
if (list)
{
list.addEventListener(CollectionEvent.COLLECTION_CHANGE,
_collectionChangeHandler);
};
};
};
};