使包装的属性可绑定

发布于 2024-11-08 13:13:28 字数 911 浏览 0 评论 0原文

我有 Class1 和一个名为 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"));
  }
}

我还有 Class2,其中包含 Class1 的私有实例代码>.我想让属性 ageClass1 可用并且仍然可绑定。

public class Class2 {
  private var c1:Class1 = new Class1();

  [Bindable????]
  public function get age():int {
  }
}

当然,那里的 [Bindable] 标记没有意义。但怎样才能达到同样的效果呢?

我相信我可以从 Class2Class1 调度某种 ageChanged 事件,然后在 Class1 中有一个事件处理程序> 调度另一个本地 ageChanged 事件,我将 Class2 的 age 属性绑定到该事件。

但这听起来不必要地复杂。难道就没有更简单的方法吗? :)

谢谢!

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!

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

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

发布评论

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

评论(2

初见 2024-11-15 13:13:28

尝试使用类似的东西:

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 {
  }
}

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 {
  }
}
彻夜缠绵 2024-11-15 13:13:28

就我个人而言,我只是将我的复合类设置为公共,并从视图中访问它(如果它是模型)(我认为是模型)。如果您不想这样做,可以随时使用 BindingUtils

在类2构造函数中

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

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