【问题标题】:Flex: make getter Bindable in an value objectFlex:使 getter 在值对象中可绑定
【发布时间】:2010-08-11 14:31:47
【问题描述】:

我在 Flex 中有一个值对象,如下所示:

[可绑定]

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;
}   

这是关于 getter 方法的。我在数据网格中显示 getter 的数据,每当我更改 ab 的某些值时,我也想更新视图。我如何实现这一目标?目前视图不会自动更新,我必须再次打开视图才能看到新值。

【问题讨论】:

    标签: apache-flex getter bindable


    【解决方案1】:

    当您创建一个属性[Bindable] 时,只要调用它的setter(即更新属性时),Flex 就会读取getter;您还没有声明任何 setter,因此 Flex 无法知道属性的值已更新。

    您必须定义both a setter and a getter method 才能将[Bindable] 标记与属性一起使用。如果您只定义一个 setter 方法,则会创建一个不能用作数据绑定表达式源的只写属性。如果您只定义一个 getter 方法,您将创建一个只读属性,您可以将其用作数据绑定表达式的源,而无需插入 [Bindable] 元数据标记。这类似于您可以使用通过使用 const 关键字定义的变量作为数据绑定表达式的源的方式。

    也许你可以定义一个空的 setter 并在你更新 a 或 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;
    

    刚刚注意到您在类而不是属性上使用了 Bindable:当您使用 Bindable tag this way 时,它使

    可用作绑定表达式的来源,您定义为变量的所有公共属性,以及通过同时使用 setter 和 getter 方法定义的所有公共属性。

    因此,除非你定义了一个 setter,否则即使整个类被声明为可绑定的,该属性也是不可绑定的。

    【讨论】:

    • 非常感谢您的详细回答。现在工作正常 :) 有时你只见树木不见森林......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多