ObjectProxy 和PropertyChangeEvent 对于类的属性变动添加监听,特别适合动态类的属性变动的监听。
记录日志处理
<mx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.events.PropertyChangeEvent; import mx.utils.ObjectProxy; import mx.utils.ObjectUtil; import mx.utils.StringUtil; private var p:ObjectProxy =null; private function application1_creationCompleteHandler():void { p= new ObjectProxy(btn); p.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updateHandler); } private function updateHandler(event:PropertyChangeEvent):void { var s:String=StringUtil.substitute("属性被修改('{0}', {1}, {2}, {3}, '{4}')", event.kind, event.property, event.oldValue, event.newValue, event.target.uid); area.text=s+event.source.toString(); } protected function btn_clickHandler(event:MouseEvent):void { btn.label="show demo"; } ]]> </mx:Script> <mx:Button id="btn" label="demo" click="btn_clickHandler(event)" /> <mx:TextArea id="area" x="0" y="30" width="418" height="213"/>
动态类的监听,一样
package com { import mx.events.PropertyChangeEvent; import mx.utils.ObjectProxy; import mx.utils.StringUtil; [Bindable] public dynamic class EmployeeRecord { } }
import mx.utils.StringUtil; import mx.events.PropertyChangeEvent; import mx.utils.ObjectProxy; import com.EmployeeRecord; private var p:ObjectProxy =null; private function showProperties():void { var emp:EmployeeRecord = new EmployeeRecord(); p.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updateHandler); p.name = "Peter Griffen"; p.age = 22; p.address = "123 St Peter Blvd, #552"; p.city = "Quahog"; p.state = "NY"; p.zip = "02593"; var displayInfo:Function = function ():String { var out:String; out = "Employee Record:\n"; out += "Name: " + emp.name + "\n"; out += "Age: " + emp.age + "\n"; out += "Address: " + emp.address + "\n"; out += "City: " + emp.city + "\n"; out += "State: " + emp.state + "\n"; out += "Zip: " + emp.zip + "\n"; return out; } } private function updateHandler(event:PropertyChangeEvent):void { var s:String=StringUtil.substitute("属性被修改('{0}', {1}, {2}, {3}, '{4}')", event.kind, event.property, event.oldValue, event.newValue, event.target.uid); }