我想从一个对象检测属性更改,然后转发该值(或重新计算该值并将结果传递给另一个对象的属性)。我在文档中看到了示例值转发的示例:
class MyModel extends Observable {
StreamSubscription _sub;
MyOtherModel _otherModel;
MyModel() {
...
_sub = onPropertyChange(_otherModel, #value,
() => notifyPropertyChange(#prop, oldValue, newValue);
}
String get prop => _otherModel.value;
set prop(String value) { _otherModel.value = value; }
}
但我不知道从哪里获取oldValue和newValue。 我想这些应该作为参数传递给onPropertyChange(第三个参数)的回调,但事实并非如此。回调不提供参数。这是疏忽还是我错过了什么?
答案 0 :(得分:0)
import 'dart:async';
import 'package:observe/observe.dart';
class MyOtherModel extends Object with Observable {
@observable
String value;
}
class MyModel extends Object with Observable {
StreamSubscription _sub;
MyOtherModel _otherModel = new MyOtherModel();
MyModel() {
///...
_otherModel.changes.listen((crs) {
crs.forEach((PropertyChangeRecord cr) =>
notifyPropertyChange(#prop, cr.oldValue, cr.newValue));
});
}
String get prop => _otherModel.value;
set prop(String value) => _otherModel.value = value;
}
void main() {
MyModel m = new MyModel();
m.prop = 'bla';
m.changes.listen(print);
// initiate change notification
Observable.dirtyCheck();
}
输出
[#<PropertyChangeRecord Symbol("value") from: null to: bla>]