Niels Bosma
Niels Bosma

Reputation: 11528

Flex: Databinding on object

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?

Upvotes: 0

Views: 1046

Answers (4)

Richard Haven
Richard Haven

Reputation: 1154

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

Upvotes: 0

Christian Nunciato
Christian Nunciato

Reputation: 10409

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!

Upvotes: 2

Andy Webb
Andy Webb

Reputation: 1710

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>

Upvotes: 0

Brandon
Brandon

Reputation: 6872

Make the class bindable:

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

Then, does this work?

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

Upvotes: 1

Related Questions