XMLListCollection events not triggered

posted in: ActionScript 3, Flex | 0

Just a quick note about the Flex 3.5 ActionScript XMLListCollection, and how not to force an update by setting a node to the contents of is self.

I needed to force an update on a collection, so that page that displayed the data was regenerated.
I wrote the following code, which was designed to set the value of the node to the value it already contains, i.e. no real change, but something that will trigger a collectionChange event.

e.g

myXMLListCollection.somenode[0].somevalue = myXMLListCollection.somenode[0].somevalue;

Doing this did produce a collectionChange event. However doing this stopped any further events being triggered on this node 🙁

e.g.
myXMLListCollection.somenode[0].somevalue = 1; Would no longer trigger a collectionChange event on myXMLListCollection.

The fix for this is to implicitly get the value of the node using toString, and hence the setter just sets the node value not the entire node linkage.

myXMLListCollection.somenode[0].somevalue = myXMLListCollection.somenode[0].somevalue.toString();

I’m not quite sure why assigning a node to its self should have this effect, but it does.

If I’d used

var s:String = myXMLListCollection.somenode[0].somevalue;

AS3 would automatically cast / convert the node value of “somevalue” to a String,

However as I was assigning back to its self, no casting would have been performed, so I must have been performing an assignment of the object of the node – to its self. Unfortunately this appears to break the onChange event handling. So it seems best not to do this !