如何从“Meteor startup”反应性地更新变量的值
How to update a variable’s value reactively from ‘Meteor startup’
我正在尝试为我正在处理的项目添加一些常规应用程序配置,我决定将其保存在数据库中,以便在应用程序出现问题时我可以从其他地方更改它们,我这样做的主要原因是每当我对应用程序进行更改或在部署期间或类似的事情时添加 'maintenance mode',我尝试这样做的方式是使用我使用此代码设置的变量:
Meteor.startup(function() {
Tracker.autorun(function () {
Meteor.subscribe('configuracion', function(){
configuracionGeneral = Configuracion.findOne({});
})
});
});
然而,当我尝试像这样使用 iron router 时:
Router.onBeforeAction(function () {
console.log(configuracionGeneral);
if(configuracionGeneral.vynoHabilitado=='habilitado'){
this.next();
}else{
//Send to maintenance template
}
});
我可以毫无问题地使用变量 configuracionGeneral,我在控制台上看到它,但是当我在数据库中更改它的值时,变量没有改变 'reactively',所以我想知道如何更改此变量 'reactively'.
我认为问题在于,according to the docs:
if the next iteration of your run function subscribes to the same
record set (same name and parameters), Meteor is smart enough to skip
a wasteful unsubscribe/resubscribe.
因此,您的订阅不会重新运行,您的回调也不会被触发。您可以尝试使用此方法,来自 David Weldon 的 common mistakes:
Meteor.startup(function() {
var handle = Meteor.subscribe('configuracion');
Tracker.autorun(function () {
if (handle.ready())
configuracionGeneral = Configuracion.findOne();
});
});
我正在尝试为我正在处理的项目添加一些常规应用程序配置,我决定将其保存在数据库中,以便在应用程序出现问题时我可以从其他地方更改它们,我这样做的主要原因是每当我对应用程序进行更改或在部署期间或类似的事情时添加 'maintenance mode',我尝试这样做的方式是使用我使用此代码设置的变量:
Meteor.startup(function() {
Tracker.autorun(function () {
Meteor.subscribe('configuracion', function(){
configuracionGeneral = Configuracion.findOne({});
})
});
});
然而,当我尝试像这样使用 iron router 时:
Router.onBeforeAction(function () {
console.log(configuracionGeneral);
if(configuracionGeneral.vynoHabilitado=='habilitado'){
this.next();
}else{
//Send to maintenance template
}
});
我可以毫无问题地使用变量 configuracionGeneral,我在控制台上看到它,但是当我在数据库中更改它的值时,变量没有改变 'reactively',所以我想知道如何更改此变量 'reactively'.
我认为问题在于,according to the docs:
if the next iteration of your run function subscribes to the same record set (same name and parameters), Meteor is smart enough to skip a wasteful unsubscribe/resubscribe.
因此,您的订阅不会重新运行,您的回调也不会被触发。您可以尝试使用此方法,来自 David Weldon 的 common mistakes:
Meteor.startup(function() {
var handle = Meteor.subscribe('configuracion');
Tracker.autorun(function () {
if (handle.ready())
configuracionGeneral = Configuracion.findOne();
});
});