Posts Tagged array-collection

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

, , ,

4 Comments