Saturday, September 1, 2012

ActionScript 3.0 Binding

Problem:
I wanted to implement the equivalent to:

<fx:Script><![CDATA[
    [Bindable]
    public var str:String = "Default literal";
]]></fx:Script>

<s:Label text="{str}" />

Solution:
Using a ChangeWatcher singleton, I could identify the property I wanted to bind to and the method to call once that value changes. In this example, I use an nameless function, but you could easily use a function name and handle the callback there. Also note that this is self-referential but could reference any object available in this context.

ChangeWatcher.watch(this, "property", function(e:Event):void{
    trace(e.target);
});

No comments: