ActionScript ArrayCollection Binding and Event Dispatching


We hear this one a lot, so I’m throwing it out there as a reminder for new and old Flex and ActionScript developers…

You’ll get some new collection data say from a remote service and you simply update your model level ArrayCollection (”AC”) property by doing:


myModel.myAC = responseFromRemoteService.myNewAC;

And low and behold the objects that are either binding to it or listening for the change event don’t do anything??? What? How the? But I debugged it and I saw that the new data was getting stored in my model-level AC…how come this thing isn’t working? Stoopid Flex…bleh.

Well, it’s simply because setting the original AC to a new AC doesn’t implicitly fire off any change events (which is the underlying implementation for data binding as well.) In order to make this function as expected, one simply needs to set the source property on an AC like so:


myModel.myAC.source = responseFromRemoteService.myNewAC.source;

The source property in an AC is not only the underlying Array for an AC, but also fires off that change event (I believe it’s the listChanged type event) that’s so important to binding and anyone else listening for changes on that AC. Truth be told, this is actually done in the underlying ListCollectionView class that an AC inherits from, but that’s just details.

Post to Twitter Tweet This Post

, , ,

  1. #1 by Steve - July 3rd, 2009 at 22:57

    Thanks! This should be posted all over the place. I can not imagine how many cumulative hours are spent on this trickiness.

    Thanks for the tip.

  2. #2 by ernest leitch - August 3rd, 2009 at 14:26

    Thank you so much for finding this. I started digging around in ArrayCollection.as and saw the listChanged event, but couldn’t find a parent object this event was attached to. This solved my problem perfectly.

  3. #3 by Aaron - August 9th, 2009 at 06:15

    Is there a way similar to listChanged, that will allow me to detect changes to an associative array?

  4. #4 by brianr - August 23rd, 2009 at 09:59

    No, b/c the framework itself doesn’t broadcast that event…the easiest way to do what you’re talking about is to either extend the Array or Dictionary class and create methods called addItem() and removeItem() and dispatch your own events in them like Event.ADD Event.REMOVE.

(will not be published)
  1. No trackbacks yet.