Updating bindings when you only change a property inside an Object

Its quite a common thing with Flex and actionscript projects to create an Object and inside that object it will have many properties.  Something in your view will be bound to the object so that the view changes with the object. So long as you change the entire object this will work fine.

Where this doesn’t work is if you change a property inside the object.

So if we have something like this

[Bindable]			
private var myObject : ObjectDataVO;
 
<view:SomeComponent
...
data="{ myObject }"
...
/>

When we set myObject to something the view component gets updated (great so far).
Lets say the myObject has a property text and the view component uses this to display some visual label, then somewhere in the app I change that property, myObject.text = “something else”;
The binding will not trigger as I haven’t actually changed the myObject, just a property inside it.

So how do we fire the binding manually? Well there is the BindingManager class (note this is an excluded class so you’ll not see it in the autocomplete ).
So in this example if I changed the myObject.text property then I could call

BindingManager.executeBindings( this, ‘myObject.text’, myObject );

This would fire of the binding as if the actual myObject had changed so anything listening in will now get updated.

[ad name=”ad-1″]

2 thoughts on “Updating bindings when you only change a property inside an Object

  1. Or you can declare your text attribute Bindable inside your ObjectDataVO

    // [Bindable] You can declare the whole class Bindable too, making every single public attribute Bindable. I don't recommend this approach, making Bindable only those attrs which have to be bindable.
    public class ObjectDataVO {
    [Bindable]
    public var text:String;
    }

    Or even, you can wrap your object with and ObjectProxy class, which implies that it’ll trigger events on ObjectDataVO text property changes.

  2. Making the property bindable doesn’t always work. It does depend on what exactly you are doing (as is always the case).
    I’d tend to stay away from making a whole class Bindable as well (unless of course I did want everything bindable – and if I did it would be better to have everything fire custom events. So I’d not mark the class as Bindable.)
    Perhaps I should have used a better example, but I was wanting a quick way to show how to use the BindingManager.executeBindings. I’ve seen a few helpful links that explain when and where using binding inside complex objects doesn’t work but can’t find them right now. Will post up a link shortly to help explain.
    The above is just another way of executing bindings when the normal methods don’t quite work.

Leave a Reply

Your email address will not be published. Required fields are marked *