views:

78

answers:

2

Hi, I am working in air application , i need to know how to add event listener when any object is updated, how can i implement this.

Example: i have Class name Vehicle, and child class are Car,Bus,Bikes,Scooter,..etc, child class also have many properties like color,model no,....etc

I have array collection and AddChild() method in Vehicle class by this, i will add, child class to the vehicle class. I need a event listener which can trigger if any of the property is updated or changed in any of the child class property, how can i implements this in Flex3.

I need this for knowing, is there any update happen in the Object.

Thanks In Advance

+3  A: 

One thing you can do is to make getters and setters for the properties instead of public vars and have your class extend EventDispatcher, if it does not already do so because it is extended from a MovieClip, like:

private var _vehicleName:String;
.
.
.
public function set vehicleName(value:String):void {
  _vehicleName = value;
  dispatchEvent(new VehicleEvent(VehicleEvent.propertyChange, "vehicleName"));
}

public function get vehicleName():String {
  return _vehicleName;
}

(VehicleEvent being an extended class of Event with an extra string to signify which property changed)

Then you can add an eventlistener to the vehicles and they will dispatch events when the properties defined in this way change.

Vincent Osinga
The setter should really be doing a check to see if the value has actually changed before setting it and dispatching an event. Otherwise, you'll get unnecessary overhead and false indications of property changes.
Rhysyngsun
A: 

If you use an Array Collection for the vehicles and if you make the properties of the vehicles Bindable as Amarghosh suggests, then the array collection already should throw an Collection Event of the kind UPDATE. It also tells you which items (Vehicles) were updated an afaik also which properties were updated. But in general it is easier to directly bind to the property, like Amarghosh says.

Jörg Reichardt