这是我目前正在使用的
class FooComponent extends Component {
constructor(...args) {
super(...args);
this.state = {
model: this.getModel()
};
}
componentWillUnmount() {
this._unmounted = true;
this._modelComputation && this._modelComputation.stop();
super.componentWillUnmount && super.componentWillUnmount();
}
getModel() {
const model = {};
this._modelComputation && this._modelComputation.stop();
this._modelComputation = Tracker.autorun((computation) => {
const { id } = this.props;
const data = id && Collection.findOne(id);
if (data) {
Object.assign(model, data);
!this._unmounted && this.forceUpdate();
}
});
return model;
}
...
}
不幸的是,反应式模型不起作用,并且Tracker.autorun
在数据库中更新模型时不执行该功能。从文档来看,Collection.findOne
应该是被动的,对吧?
我不确定我做错了什么。为什么不Tracker
监控数据库模型?Collection.findOne
为什么当数据库更改时函数不重新评估?
** 编辑 **
更新数据库时,我确实看到集合通过 更改meteortoys:allthings
,但autorun
没有重新执行。