Flex:使 getter 在值对象中可绑定

发布于 2024-09-14 08:58:25 字数 645 浏览 6 评论 0 原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-09-21 08:58:25

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

您必须定义setter 和 getter方法 将 [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 而不是属性:当您使用 以这种方式绑定标签,它使得

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文