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

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

问题描述

帮助我了解数据绑定

当我在类中创建一个变量时:


[Bindable] private var _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中。它执行此操作,以便在设置新值时,wrapper 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中的Getters / setters被认为是对象的属性(它们不是目的)。因此,您是否将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天全站免登陆