当前位置: 动力学知识库 > 问答 > 编程问答 >

flex - Make a wrapped property bindable

问题描述:

I have Class1 with a read-only bindable property called age:

public class Class1 {

private var _age:int;

[Bindable(event="ageChanged"]

public function get age():int {

return this._age;

}

public function something():void {

_age++;

dispatchEvent(new Event("ageChanged"));

}

}

I also have Class2 which contains a private instance of Class1. And I want to make property age available from Class1 and still be bindable.

public class Class2 {

private var c1:Class1 = new Class1();

[Bindable????]

public function get age():int {

}

}

Of course the [Bindable] tag there doesn't make sense. But how can I achieve the same effect?

I believe I can dispatch some sort of ageChanged event from Class2 up to Class1 and then have an event handler in Class1 dispatch another local ageChanged event to which I bind Class2's age property.

But this sounds unnecessarily complicated. Isn't there a simpler way? :)

Thanks!

网友答案:

Personally, I've just put my composite class as public and access it from the view if it's a model (which i think it is). If you don't want to do that, you can always use BindingUtils:

In the class 2 constructor

var watcher:ChangeWatcher = BindingUtils.bindProperty(this, 'age', c1, 'age');
网友答案:

Try to use something like:

public class Class2 extends EventDispatcher {
private var _c1:Class1;

public function set c1(value:Class1):void
{
  if (value == _c1)
    return;
  if (_c1)
    _c1.removeEventListener("ageChanged", ageChangedHandler);
  _c1 = value;
  if (_c1)
    _c1.addEventListener("ageChanged", ageChangedHandler);
}

private function ageChangedHandler(event:Event):void
{
  dispatchEvent(new Event("ageChanged"));
}

  [Bindable("ageChanged")]
  public function get age():int {
  }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: