1
votes

On one PC, I have a datagrid with dataprovider being an arraycollection populated by a collection of records retrieved from MySQL DB. On another PC, another component retrieve the same collection of records and update them. Instanly, the datagrid in PC one reflected the changes that were made in PC two which is nice because it pushes the changes to all affected controls online. However, I want to detect the the arraycollection changes to do some other things but such CollectionEvent.COLLECTION_CHANGE is not detected. Can you help please? Here is the code:

protected function doInit():void
{
acLeave.addEventListener(CollectionEvent.COLLECTION_CHANGE, onAcLeaveChange);
}

protected function onAcLeaveChange(event:CollectionEvent):void
{
do something
}

I am using lcds and the data management service handled the data synchronization already. That is why the first pc with the data grid data provider acLeave changed automatically. Somehow it is because lcds knows there is a client (pc one) online, then it pushes the changes to it. My question is that the datagrid data changed, I want to detect there is a data change occur so that I can do some other updates. Normally, to detect datagrid change, I can use datagrid datachange or simply addlistener to the data provider for collectionEvent.COLLECTION_CHANGE but in this case even though I can see the ac acLeave changed, the event did not fire. Please help!

Hi, me again and thanks for your advise. I have added the setter to acLeave but still unable to listen the collectionEvent change. Here is the modified code:

private var _acLeave:ArrayCollection = new ArrayCollection();

[Bindable]
public function get acLeave():ArrayCollection
{
    return _acLeave;
}
public function set acLeave(value:ArrayCollection):void
{
    _acLeave = value;
}
protected function doInit():void
{
    acLeave.addEventListener(CollectionEvent.COLLECTION_CHANGE, onAcChange);
}

protected function dataGrid_creationCompleteHandler(event:FlexEvent):void
{
    getAllResult.token = leaverequestService.getAll();
    getAllResult.addEventListener(ResultEvent.RESULT, onGotResult);
}

protected function onGotResult(event:ResultEvent):void
{
    acLeave = getAllResult.lastResult;
}

protected function onAcChange(event:CollectionEvent):void
{
    // this never executed because unable to detect a change on acLeave
    Alert.show("acLeave Changed !");
}
1
The only way two separate PCs can reflect changes in the same "data soruce" is through a server; most likely with some type of push functionality enabled. Changing an item on one PC will not fire a COLLECTION_CHANGE event on the other. - JeffryHouser
I agree w/Flextras: how is the remote collection (on the other PC) getting updated? It's possible you listen for the event, and when the update occurs a whole new ArrayCollection gets generated and sent to (or retrieved by) the 2nd PC, rather than the original collection being changed. If something like that happened no change event would be generated by the original collection. - Sunil D.

1 Answers

0
votes

If your collection change handler is not firing, I am guessing something like this is happening:

  • pc1 adds collection change listener
  • pc2 changes the collection
  • LCDS sends pc1 a brand new ArrayCollection object (not just the element that changed) to use

The original ArrayCollection you are listening for the collectionChange is not necessarily getting changed, it is getting replaced. So no collection change event ocurrs.

If you add a setter method for this ArrayCollection (acLeave in your example), then you will know if this ever happens. Technically, you should use both the collection change event and this setter to be able to detect all the cases when the array could be changed.