我在actionscript中有一个Object,它有几十个属性,每个属性都被定义为可绑定并且有自己的change事件.我想听取对该对象所做的任何更改,而无需为其所有属性添加侦听器.在actionscript中是否有一种方法可以用来监听对象值的任何变化?
谢谢
您可以在[Bindable]类上使用PropertyChangeEvent来侦听任何属性更改.只要您使用get/set属性.
package { [Bindable] public class Person { private var _firstName:String; private var _lastName:String; private var _age:Number; public function get firstName():String { return _firstName; } public function set firstName(value:String):void { _firstName = value; } public function get lastName():String { return _lastName; } public function set lastName(value:String):void { _lastName = value; } public function get age():Number { return _age; } public function set age(value:Number):void { age = value; } public function Person() { // empty constructor } } }
然后,在您的using类中添加事件侦听器.
public var p:Person; private function addListener():void { p = new Person(); p.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onPropertyChange); } private function onPropertyChange(event:PropertyChangeEvent):void { trace(event.property + " " + event.kind + " " + event.oldValue + " " + event.newValue); }