数据绑定的依赖关系

时间:2010-08-20 20:00:27

标签: flex data-binding

我想将一个派生属性添加到flex:

 private var a:int;
 private var b:int;
 [Bindable]
 public function get derived():int
 {
     return a+b;
 }

但是,当我更改a或b时,派生不会更新。是否有更好(更快)的方法来进行派生更新,而不是使用a和b setter方法使其无效?

编辑:添加了关键字“get”。现在,这更有意义,我希望。

3 个答案:

答案 0 :(得分:2)

您的代码不会创建属性,而是创建方法。方法不能是Bindable,只能是属性。这种方法应该适合你:

 private var _a:int;
 public function get a():int{
 return _a
 }
 public function set a(value:int):void{
   _a = a; 
   dispatchEvent(new Event('derivedChanged'));
 }
 private var _b:int;
 public function get b():int{
 return _b
 }
 public function set b(value:int):void{
   _b = b; 
   dispatchEvent(new Event('derivedChanged'));
 }

 [Bindable(event="derivedChanged")]
 public function get derived():int
 {
     return a+b;
 }

我在浏览器中编写代码;所以可能会有轻微的语法错误。

答案 1 :(得分:1)

您可以在派生函数上使用[Bindable(event="propertyChanged")]

您还应该使派生函数成为一个吸气剂。

我应该工作因为flex使用PropertyChangeEvent.PROPERTY_CHANGE绑定变量,通过自动创建getter和setter并调度PropertyChangeEvent。修改a或b将自动使derived的结果无效。

答案 2 :(得分:0)

我觉得这是一个常见的问题,所以我写了一些代码来帮助它。您可以在Bindable元数据中添加getter所依赖的内容。所以:

[可绑定(事件= “derivedChanged”,dependentProperty = “A”)]  [可绑定(事件= “derivedChanged”,dependentProperty = “B”)]  public function get derived():int  {      返回a + b;  }

这是使用Parsley的元数据处理编写的自定义代码,但你可以在没有Parsley的情况下使用它 - 它只是一个普通的方法调用,看起来不太好。

检查出来:http://frishy.blogspot.com/2011/06/binding-dependencies.html

-Ryan

相关问题