0

Lets say I have the following:

public class MyObject
{

[Bindable]
public var foo:int = 3;


}

...

[Bindable]
var _obj:MyObject = null;

What's the best way to bind foo so that this binding is updated when _obj is set to a new instance?

I've tried:

<mx:Label text="{_obj&amp;&amp;_obj.foo}"/>

But it isn't pretty. Can I express this somehow using

What do the SO::Flex community think?

4 Answers 4

2

Binding doesn't depend on properties; you can get the benefits of binding with either fields or properties.

It looks like you might've left something out, maybe? The following MXML and AS, for example, work to bind on both a field and a property, once the class and its instance are marked Bindable:

package
{
    [Bindable]
    public class MyFoo
    {
        public var myPublicField:int;
        private var _myPrivateField:int;

        public function MyFoo()
        {
            myPublicField = 0;
            myPublicProperty = 0;
        }

        public function get myPublicProperty():int
        {
            return _myPrivateField;
        }

        public function set myPublicProperty(value:int):void
        {
            _myPrivateField = value;
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Script>
        <![CDATA[

            [Bindable]
            private var myFoo:MyFoo;

            private function increment():void
            {
                if (!myFoo)
                    myFoo = new MyFoo();

                myFoo.myPublicField += 1;
                myFoo.myPublicProperty += 1;
            }

            private function replace():void
            {
                myFoo = new MyFoo();
            }

        ]]>
    </mx:Script>

    <mx:VBox>
        <mx:HBox>
            <mx:Label text="Field:" />
            <mx:Text text="{myFoo.myPublicField}" />
        </mx:HBox>
        <mx:HBox>
            <mx:Label text="Property:" />
            <mx:Text text="{myFoo.myPublicProperty}" /> 
        </mx:HBox>
        <mx:Button label="Increment Both Counts" click="increment()" />
        <mx:Button label="Replace with New Foo" click="replace()" />
    </mx:VBox>

</mx:Application>

Incidentally, the effect would be the same if you marked the field and the property Bindable separately, rather than marking the class, which is just shorthand for the same thing. Also note myFoo starts out null by default, as in your sample code (i.e., the " = null" assignment is redundant).

Does this example help? If not, leave a comment and I'll check back. Hope it does!

1

Make the class bindable:

[Bindable]
public class MyObject
{
    [Bindable]
    public var foo:int = 3;
}

Then, does this work?

<mx:Label text="{_obj.foo}"/>
1
  • No, it didn't. Assignments to _obj doens't update the label (at least in my case). Jan 5, 2009 at 19:21
0

Use properties instead of public variables.

// Object

package MyObject
{
    public class MyObject
    {
        private var message:String;

        public function set Message(messagein:String):void
        {
            message = messagein;
            return;
        }

        [Bindable]
        public function get Message():String
        {
            return message;
        }

        public function MyObject()
        {
        }

    }
}

// Flex that calls it

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="281" height="156">
<mx:Script>
    <![CDATA[
        import MyObject.MyObject;
        [Bindable]
        private var o:MyObject = new MyObject();

        private function UpdateMessage():void
        {
            o.Message = TextIn.text;
            return;
        }
    ]]>
</mx:Script>
    <mx:Label x="31" y="29" text="{o.Message}" width="201" id="TextOut"/>
    <mx:TextInput x="31" y="55" id="TextIn"/>
    <mx:Button x="31" y="85" label="Button" click="UpdateMessage()"/>

</mx:Application>
1
  • yes but what happens with the binding when you set o.Message = new MyObject() somewhere else in the code. Jan 5, 2009 at 22:31
0

I would look at the mx.binding.utils.BindingUtils class. You might find something there, but (as Christian Nunciato says above) data binding is really built for changes in a property of an object, not the object itself.

Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.