【发布时间】:2014-06-18 03:47:27
【问题描述】:
我正在尝试使用观察包而不必在我的模型中添加注释,并且只能通过在设置器中提高 notifyPropertyChange 来进行测试,因此我进行了以下测试
import 'package:observe/observe.dart';
import 'dart:async';
import 'dart:math';
void main() {
var dummyWatchingModel = new DummyWatchingModel();
new Timer.periodic(new Duration(milliseconds:1000), (_){
//calls a function that set a random value to the property in the observable model
dummyWatchingModel.setModelProps();
});
}
class Model extends Observable{
int _x;
Model(this._x);
int get x=> _x;
void set x(int value){
_x = notifyPropertyChange(#_x, _x, value);
}
}
class DummyWatchingModel{
Model model = new Model(1);
final rng = new Random();
anotherModel(){
//watch for changes in model instance properties
this.model.changes.listen((List<ChangeRecord> records) {
for(ChangeRecord change in records){
print(change.toString());
}
});
}
//the callback for the timer to assign a random value model.x
setModelProps(){
model.x = rng.nextInt(100);
print('called...');
}
}
我正在使用引发 notifyPropertyChange 的 setter 更改 Model 实例中的属性值,但它从不监听更改,知道为什么吗?
【问题讨论】:
标签: dart dart-polymer