开发者

What is the best way to "listen" on a setter?

开发者 https://www.devze.com 2023-02-20 13:09 出处:网络
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 ?

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) {
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消