Still having some problems understanding the concept of Reactive-Var.
I have a collection AppSettings. When I access it via AppSettings.findOne().settingX everything works reactive: if I change the value in the database, the methods that use settingX are using the updated value.
If I want to have the results of AppSettings.findOne() in a variable now, which is again reactive, how can this be set up?
I tried:
MyAppSettings = new ReactiveVar(AppSettings.findOne());
However, whenf I call MyAppSettings.get().settingX it does not react to changes in the database. Do I need to put this in Meteor.autorun or something?
Yes. In Meteor, reactive variables are only reactive if they’re used in a reactive environment (a function passed to Tracker.autorun (which was former known as Meteor.autorun) is a reactive environment in Meteor). So, in your case, you would need to do something like this:
MyAppSettings = new ReactiveVar()
Tracker.autorun(function(){
MyAppSettings.set(AppSettings.findOne());
})
I wonder what would be the use case for a reactive var wrapping a mongo request ?
Also, when you are doing MyAppSettings = new ReactiveVar(AppSettings.findOne()); you are only setting initial value, not subsequent one, is that really what your are trying to do ?
@Peppe_LG thanks for the clarification. @vjau you are right. I thought that moving app settings to a variable and then be able just call AppSetting.settingX, AppSetting.settingY at the relevant places, would be a little nicer than calling findOne() each time.