PDA

View Full Version : ArrayCollection not throwing update events


craigp
12-24-2008, 05:03 PM
Edit: Being new, I think I posted this in the wrong zone. I guess this should be here: http://www.actionscript.org/forums/forumdisplay.php3?f=75

I'm trying to bind to an ArrayCollection, but I find that the thing won't throw any update events. It works fine with strings, for example, but not ArrayCollections. I'm using Adobe(R) Flex(TM) Builder(TM) 3 (TM)(R), version 3.0 (build 3.0.2.213193 (TM))

The documentation specifically says ArrayCollections should throw update events when you use addItem, so does anyone know what's up?

To show you what I mean, I built a simple class to demonstrate. It updates a String and an ArrayCollection at the same time and watches for updates to both. The only "update" event it catches for the ArrayCollection is the initial one from the first binding. Here's the trace log after initializing the object, then calling the alter function three times. Investigating the actual values shows that the ArrayCollection is successfully updated: it just doesn't throw the event.


Caught ArrayCollection event
Caught string event
Altered data (should cause events)
Caught string event
Altered data (should cause events)
Caught string event
Altered data (should cause events)
Caught string event

Here's the class, with exasperated demo names left intact:

public class ADemo
{
[Bindable]
public var ac:ArrayCollection = new ArrayCollection();
[Bindable]
public var argh:String = "";

public function ADemo()
{
ac.enableAutoUpdate();
BindingUtils.bindSetter(updatedAC, this, "ac");
BindingUtils.bindSetter(updatedString, this, "argh");
}

public function alter():void
{
trace("Altered data (should cause events)");
ac.addItem({name: "Wheee!"});
argh += "MOJO";
}
public function updatedString(obj:Object = null):void
{
trace("Caught string event");
}
public function updatedAC(obj:Object = null):void
{
trace("Caught ArrayCollection event");
}


}

drkstr
12-24-2008, 07:45 PM
You are creating a binding to the ac property itself, and not to the underlying ArrayCollection. This binding would only execute if you assign this.ac = new ArrayCollection(). You will need to listen for a collectionChange event on this.ac and dispatch it out of your class if you want it to be made external.


Best Regards,
~Aaron

craigp
12-24-2008, 08:12 PM
You are creating a binding to the ac property itself, and not to the underlying ArrayCollection. This binding would only execute if you assign this.ac = new ArrayCollection(). You will need to listen for a collectionChange event on this.ac and dispatch it out of your class if you want it to be made external.

I was worried that might be the case. I'll look into it.

drkstr
12-24-2008, 08:44 PM
FYI, this should execute the bindings externally:


public function ADemo() {
this.ac.addEventListener( 'collectionChange', handleCollectionChange );
}

private function handleCollectionChange(event:Event): void {
dispatchEvent('propertyChange');
}

Best Regards,
~Aaron