开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜