What is the best way to "listen" on a setter?
I w开发者_开发问答ant a custom function called after the setter call. Do you have an idea ? An ObjectProxy on variables with PROPERTY_CHANGE listener is the only way ?
Thanks
Extend EventDispatcher from your class, then in your set method simply dispatch an event.
class MyClass extends EventDispatcher {
public static const PROPERTY_CHANGED:String = "PROPERTY_CHANGED";
private _foo:Number = 0;
public function MyClass() { }
// else where
public function set foo(value:Number):void {
_foo = value;
dispatchEvent(new Event(PROPERTY_CHANGED));
}
}
I actually prefer a mechanism that I think is a bit more declarative and clean:
[Bindable]
public class MyClass
{
public var foo:Number;
public function MyClass() {
BindingUtils.bindSetter(whenFooIsSet, this, "foo");
}
private function whenFooIsSet(newValue:Number) {
}
}
精彩评论