1 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

Using [Bindable] & ChangeWatcher in ActionScript-only classes

What are you talking about?

Recently I’ve been toying around with using [Bindable] Metadata in Flex and AS3, namely trying to not only make it so that my static variables can be used as bindings in Flex, but also that I can detect when the variables change.

How do you do that?

There’s a few ways you can do that, and a whole plethora of other blogs that will give you rundowns on how.  What you need to look for are the ChangeWatcher and BindingUtils classes [livedocs].  There doesn’t seem to be anything in particular thats different between them, ChangeWatcher lets you define a variable and function to call on changes, and BindingUtils lets you define functions to call when variables are set or properties change – all in all, not much difference.

But…

What none of the examples mention however, is that all of the tutorials on this are covering script in MXML components or extending existing MXML components and are therefore all using the Flex framework.  You can add in all the ChangeWatcher.watch()’s you want and get no errors, but if you’re not extending the Flex framework, nothing is going to work!

So.

So, if you want to detect binding changes in an ActionScript-only class, you need to do it in something that’s extending a Flex component, otherwise it just won’t work!  Obvious really, but it took me a good few hours to figure out.  I haven’t done extensive testing to see what the lowest level component to extend was, but I’ve plumbed for extending the UIComponent, and all of my binding detections work.

pulic class ImageSizerApplicationConfig extends UIComponent

4 Comments »

  1. That’s not actually correct. [Bindable] does not use any Flex Framework specific API (beyond the PropertyChangeEvent class), and the BindingUtils is similar. You do not have to derive from UIComponent or use this only in the context of MXML. I’d recommend watching the binding “deep dive” session from 360|Flex.

    Comment by Troy Gilbert — 14 July 2009 #

  2. Thanks for the link & clarification Troy.

    I guess I misunderstood what the Flex framework actually ‘was’ – and where the framework starts and stops with regards to visual / utilities classes.

    Comment by James — 15 July 2009 #

  3. You might consider using a get and set and then just appending the Bindable attribute on the get method. something like:

    [Bindable]
    public function get property():Object{}
    public function set property(o:Object){}

    Then you can dispatch listeners in your set and you can listen to them just fine in your AS3 code.

    Comment by Morgan Engel — 9 December 2009 #

  4. Yet if [Bindable] does not use any Flex framework in an ActionScript project in Flashdevelop or Flashbuilder this fails:

    [code]package
    {
    import flash.display.Sprite;
    import flash.events.EventDispatcher;
    import flash.events.Event;

    public class BindableTest extends Sprite
    {
    private var _maxFontSize:Number = 15;

    public function BindableTest() {
    this.maxFontSize = 100;
    }

    [Bindable(event="maxFontSizeChanged")]
    public function get maxFontSize():Number {
    trace(" max ");
    return _maxFontSize;
    }

    public function set maxFontSize(value:Number):void {
    _maxFontSize = value;
    var eventObj:Event = new Event("maxFontSizeChanged");
    dispatchEvent(eventObj);
    }

    }
    }
    [/code]

    Any clues!?

    Comment by Thomas James — 10 June 2010 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: This post is over a year and a half old. You may want to check later in this blog to see if there is new information relevant to your comment.