views:

625

answers:

2

I would like to put an eventListener on an ArrayCollection's length proprety, but I don't know how to go about this.

I want to do this because I only want code to execute when a certain number of things are in a certain ArrayCollection. I want Flex to wait to execute this code over the next object until that length property drops back to an acceptable level. I think I should do this with events instead of a while loop that sits there spouting NOOPs forever (which I don't know how to do either).

Please help, and thanks in advance. SO has been a great help so far.

A: 

You could just add a 'collectionChange' event listener and check if the ArrayCollection.length has changed since the previous event. (You'd need to keep a static or instance copy of the ArrayCollection's length, for comparison.)

The other thought is to create your own lengthChangedEvent, extend ArrayCollection, override the length property and look for then call any lengthChangedEvent listeners in the setter. Then use your extended ArrayCollection in place of the base class wherever you need to listen for the event. This won't work though because the length property is read-only and there is no setter to override. You would have to override every method that could add or remove items from the ArrayCollection and test/call your lengthChangedEvent listener in each case.

John Lemberger
+2  A: 

ArrayCollection will dispatch a "collectionChange" event when its items changed. So you can listen to that event and check the "length" property each time the event is dispatched. Alternatively you can also just bind to the length property via BindingUtils.bindSetter();

Christophe Herreman