views:

162

answers:

1

I have a collection of objects and each object throws an event every time its value gets updated. Im trying to capture that event by adding a listener to the arraycollection that holds it (see main class) but its not working. Honestly I'm not sure this is the correct approach.

I'm avoiding using Collection.CHANGE because it fells into an infinite recursion ultimately ends in a stack overflow. Any ideas?


[Bindable]
public class NamesVO {  
 public var steveList:ArrayCollection; // array of SteveVO objects

 public function NamesVO() {
  steveList = new ArrayCollection();
 }  

 public function rename():void {
  for each(var steve:SteveVO in steveList) { 
   steve.rename();
  }   
 }
}


[Bindable]
public class SteveVO extends EventDispatcher {
    public static const VALUE_CHANGED:String = "VALUE_CHANGED";

 public var code:String;
 public var name:String;
 public var _quantity:Number;

 public function SteveVO() {
  this.code = "";
  this.name = "";
  _quantity = 0;
 }

 public function get quantity():Number {
  return _quantity; 
 }

 public function set quantity(quantity:Number):void {
  _quantity = quantity;
  dispatchEvent(new Event(VALUE_CHANGED)); 
 }

 public function rename():void {
  name = code + " - " + _quantity;
 }  
}


Main class:

names = new NamesVO();
names.steveList.addEventListener(SteveVO.VALUE_CHANGED, function():void {
     names.rename();  // this anon function is not being executed!!
}); 

var steve:SteveVO = new SteveVO();
names.steveList.addItem(steve); 

// names is bound on a datagrid and uses itemeditor for each SteveVO object
A: 
Is there a reason to change all the names when one quautity is changed. Would it be better simply to call rename inside the set function of the steveVO class?--Yes. name is just an example. im planning to add fields like percentage. everytime the quantity changes, the percentage also changes, based on a given "total quantity".
Collection.CHANGE is the only event i know to detect the changes in arraycollection. how do i implement that? I tried new Event(VALUE_CHANGED, true) // to bubblebut didnt work either.