Watch for changing properties in Flex

In AS3 there is a handy utility class called ChangeWatcher, which lives inside the mx.binding.utils package. It provides you with a handy way of watching other object’s properties.

Here’s how it’s used:, "property", handler);
the object which owns the property
the property you want to watch
the function name of the handler

Whenever the property of the object changes, the handler will respond. Quite handy, right?

Here’s an example. Let’s say we have a class called WatchMe. Inside that class we have a public variable called ImBeingWatched. We’d like to trigger a function whenever this variable changes. To do this, we simply add one line of code:

    import mx.binding.utils.*;
    import flash.display.*;

    public class WatchMe extends MovieClip
        [Bindable] public var ImBeingWatched:Boolean = true;

        public function WatchMe():void
  , "ImBeingWatched", doSomething);

        private function doSomething(e:Event):void
            // do something ...

The property that ChangeWatcher is watching must be [Bindable] and Public for it to work.

Latest articles