views:

434

answers:

5
+1  A: 

Make the class bindable:

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

Then, does this work?

<mx:Label text="{_obj.foo}"/>
Brandon
No, it didn't. Assignments to _obj doens't update the label (at least in my case).
Niels Bosma
A: 

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>
Andy Webb
yes but what happens with the binding when you set o.Message = new MyObject() somewhere else in the code.
Niels Bosma
+2  A: 

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!

Christian Nunciato
A: 

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

Richard Haven
A: 

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

Richard Haven