Flex:帮助我理解 getter 和 setter 上的数据绑定 [英] Flex: Help me understand data Binding on getters and setters

查看:22
本文介绍了Flex:帮助我理解 getter 和 setter 上的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮我理解数据绑定
当我在类中创建变量时:

[可绑定] 私有变量 _name:String;

然后生成 getter 和 setter,我得到:

Help me understand data Binding
When I create a variable in a class:

[Bindable] private var _name:String;

and then generate getters and setters, I get:

            private var _name:String;

            [Bindable]
            public function get name():String
            {
                return _name;
            }

            public function set name(value:String):void
            {
                _name = value;
            }

为什么它只在get函数上生成标签'[Bindable]'?
对我来说,它似乎应该在 set 函数上,因为我想知道什么时候值发生变化,而不是在刚刚读取值时.

Why does it generate the tag '[Bindable]' only on the get function?
To me, it would seem that it should be on the set function, as I would want to know when the value changes, not when the value is just read.

推荐答案

可能有助于理解此处发生的事情的是 MXML 编译器在您制作 [Bindable] 时将为您生成的代码.MXML 编译器将您的 [Bindable] 属性包装在它自己的 getter/setter 中.它这样做是为了在设置新值时包装器设置器方法可以调度propertyChange"事件.此事件通知绑定到属性的各方该值已更改.

What might help to understand what is going on here is the code that the MXML compiler will generate for you when you make something [Bindable]. The MXML compiler wraps your [Bindable] property in it's own getter/setter. It does this so that the wrapper setter method can dispatch a "propertyChange" event when a new value is set. This event notifies the parties binding to the property that the value has changed.

Actionscript 中的 Getter/setter 被认为是对象的属性(它们不是对象的方法).因此,无论您将 getter 或 setter 注释为 [Bindable],生成的代码都会做正确的事情.

Getters/setters in Actionscript are considered to be properties of the object (they are not methods of the object). So it doesn't matter whether your annotate the getter or the setter as [Bindable], the generated code does the right thing.

值得注意的是,您可以通过在您的属性更改时调度您自己的事件来避免生成代码并优化情况.为此,您的 [Bindable] 元数据标签需要包含将在属性更改时调度的事件名称:

It's worth noting that you can avoid the generated code and optimize the situation by dispatching your own event when your property changes. To do this, your [Bindable] metadata tag needs to include the event name that will be dispatched when the property changes:

private var _name:String;

[Bindable("nameChanged")]
public function get name():String
{
    return _name;
} 

public function set name(value:String)
{
    if (_name == value)
        return;
    _name = value;
    dispatchEvent(new Event("nameChanged"));
}

因为可绑定元数据包含一个事件字符串,所以不会生成额外的代码.请注意,如果您忘记从 setter 分派事件,编译器不会警告您.事实上,您可以从类中的任何位置分派自定义绑定事件(这对于可绑定的函数很有用).

Because the bindable metadata contains an event string, no extra code is generated. Note, the compiler won't warn you if you forget to dispatch the event from the setter. In fact, you can dispatch your custom binding event from anywhere in your class (this can be useful with functions that are bindable).

这篇关于Flex:帮助我理解 getter 和 setter 上的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆