Javascript Tracker.autorun只运行一次
以下是我目前正在处理的问题Javascript Tracker.autorun只运行一次,javascript,meteor,meteor-tracker,Javascript,Meteor,Meteor Tracker,以下是我目前正在处理的问题 class FooComponent extends Component { constructor(...args) { super(...args); this.state = { model: this.getModel() }; } componentWillUnmount() { this._unmounted = true; this._modelComputation &&am
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
在数据库中更新模型时不执行该功能。从文档来看,应该是被动的,对吗
我不确定我做错了什么。为什么Tracker
不监视数据库模型?当数据库发生变化时,为什么函数不重新评估Collection.findOne
**编辑** 更新数据库时,我确实看到集合通过
meterotoys:allthings
更改,但autorun
不会重新执行。查看如何实现它,我将代码更改为这样
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.nonreactive(() => {
return 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;
}
...
}
我不完全明白为什么,但它现在起作用了。这有点模糊。您希望触发自动运行功能的具体情况是什么?你试过了吗?@Alex从我那里,
findOne
应该是被动的,因此数据库中的任何更改都应该使计算无效并重新执行自动运行
功能,不是吗?如前所述,它不起作用,并且在数据库更改时不会调用该函数。