How to make custom AS3 classes available for data binding?

Suppose I have a class costup actionscript.

public class myClass

{
       private var myVariable:ArrayCollection;
...
}

Suppose also that I have another class that changes a second variable that has [Bindable] metadata. What methods and events should I implement in any of these classes to change myVariable when the other changes?

+3
source share
1 answer

If you make it myVariablepublic, you can simply use [BindingUtils.bindProperty()][1]:

public class MyClass
{
    public var myVariable:ArrayCollection;

    public function MyClass(other:OtherClass) {

        BindingUtils.bindProperty(this, "myVariable", other, "propertyName");

    }
}

If you prefer to keep myVariableprivate, you can use [BindingUtils.bindSetter()][2]:

public class MyClass
{
    private var myVariable:ArrayCollection;

    public function MyClass(other:OtherClass) {

        BindingUtils.bindSetter(
            function(newVal:*):void {
                this.myVariable = newVal;
            }, other, "propertyName");

    }
}
+1
source

All Articles