views:

19

answers:

1

I have an value object in Flex which looks like:

[Bindable]

public class MyVO
{
    public var a:ArrayCollection;
    public var b:ArrayCollection;
    private var timeSort:Sort;

    public function ShiftVO(){
        timeSort = new Sort();
        timeSort.compareFunction = sortDates;
    }

public function get times():ArrayCollection{        
    var ac:ArrayCollection = new ArrayCollection(a.toArray().concat(b.toArray()));

    ac.sort = timeSort;
    ac.refresh();

    return ac;
}   

It's about the getter method. I display the data of the getter in a datagrid and whenever I change some values of a or b I want to update the view as well. How do I achieve this? Currently the view doesn't update itself automatically, I have to open up the view again to see the new values.

+1  A: 

When you make a property [Bindable], the Flex will read the getter whenever its setter is called (ie, when the property is updated); you haven't declared any setter and hence there is no way for Flex to know that the value of property has been updated.

You must define both a setter and a getter method to use the [Bindable] tag with the property. If you define just a setter method, you create a write-only property that you cannot use as the source of a data-binding expression. If you define just a getter method, you create a read-only property that you can use as the source of a data-binding expression without inserting the [Bindable] metadata tag. This is similar to the way that you can use a variable, defined by using the const keyword, as the source for a data binding expression.

May be you can define an empty setter and call it whenever you update a or b.

public function set times(ac:ArrayCollection):void { }

//somewhere else in the code:

a = someArrayCol;
/** 
 * this will invoke the setter which will in turn 
 * invoke the bindable getter and update the values 
 * */
times = null;

Just noticed that you're using Bindable on the class instead of the property: when you use the Bindable tag this way, it makes

usable as the source of a binding expression all public properties that you defined as variables, and all public properties that are defined by using both a setter and a getter method.

Thus, unless you define a setter, the property is not bindable even if the whole class is declared as bindable.

Amarghosh
thank you very much for your detailed answer. Works fine now :)Sometimes you can't see the forest for the trees...
hering