2

目前我有一个计算的吸气剂 xyz。为了安排新的计算,我调用notifyProperty(this, #xyz);.

在最新版本的observe中,notifyProperty不推荐使用。我该如何更换它?文档建议使用this.notifyPropertyChange(#xyz, oldValue, newValue);. 问题是,在计算 getter时,我没有oldValue(而不是直接的)。newValue

4

1 回答 1

4

开发人员的建议是将oldValue周围保存在私有变量中以供参考。至于newValue你实际上可以只传递getter,它会用新值计算它。

您将看到与此类似的课程:

class MyElement extends Observable {
  @observable var foo, bar;
  var _oldValue;
  @reflectable get xyz => foo + bar;

  MyElement() {
    // we use the xyz getter to compute its own new value
    // notifyPropertyChange returns the new value for convenience :)
    notifyXyz() { _oldValue = notifyPropertyChange(#xyz, _oldValue, xyz); }
    onPropertyChange(this, #foo, notifyXyz);
    onPropertyChange(this, #bar, notifyXyz);
  }
}
于 2013-10-23T18:12:16.553 回答