Here is what I have encountered using ReactiveVar…
Template.Test.onCreated(function(){
const testDefault = { one: 1, two: 2 };
console.log('print - 1', testDefault);
// prints ‘{ one: 1, two: 2 }’ - OK
this.testReactive = new ReactiveVar(testDefault);
console.log('print - 2', testDefault);
// prints ‘{ one: 1, two: 2 }’ - OK
// now here the fun starts..
const testVariable = this.testReactive.get();
testVariable.two = 10;
console.log('print - 3', this.testReactive.get());
// it prints { one: 1, two: 10 } even thought I did not set the reactiveVar specifically, expected { one: 1, two: 2 }, not OK
console.log('print - 4', testDefault);
// it prints { one: 1, two: 10 } instead of expected { one: 1, two: 2 }, not OK
});
So how come changing the testVariable
changes the testDefault
variable? Is it ‘normal’? Any ideas, comments?
I have managed to solve it by cloning:
const testVariable2 =_.clone(this.testReactive.get());
testVariable2.two = 10;
console.log('print - 3', this.testReactive.get());
console.log('print - 4', testDefault);
// it prints { one: 1, two: 2 } in both cases, as expected - OK
But this looks weird and not reliable, as one can forget to clone something and get unintended mutations of initial ‘default’ variables.
You would probably suggest using ReactiveDict for that, and most likely I will have to do so, thought I wanted initially to keep things simple, as in this case I just need an object with 2 values that I will be changing only together.