Session variables: to use or not to use

I am a bit concerned with the use of Session variables. I am normally using them to control de UI, e.g. adding classes or hidding elements.

However after reading Large Meteor projects - best practices I kind of agree with his opinion about Session variables.

In the case of UI manipulation, would it be better to user jQuery for example?

Which are your thoughts?

1 Like

I would use a minimal amount of jquery. Instead of Session, you would use ReactiveVar and ReactiveDict, scoped to your template

Template.myTemplate.onCreated(function () {
    this.toggle = new ReactiveVar(false);
});

Template.myTemplate.helpers({
    toggle: function () {
         var instance = Template.instance();
         return instance.toggle.get();
    }
});

Template.myTemplate.events({
    "click .toggle": function (event, instance) {
         instance.toggle.set(!instance.toggle.get());
    }
});

Nice!

I like this solution!

Cheers

But what about when you need to use the ReactiveVar outside the scope of the template? In this situation I find myself using Session variables but I feel bad about it.

Eg. Imagine you had an array of toggles and they were shown in a list parentListTemplate.

Template.myTemplate.onCreated(function () {
    this.toggle = new ReactiveVar(false);
});

Template.myTemplate.events({
    "click .toggle": function (event, instance) {
        instance.toggle.set(!instance.toggle.get());
    }
});

Template.parentListTemplate.helpers({
    everythingIsToggledOn: function() {
        return ?
    },
    atLeastOneToggleIsOn: function() {
        return ?
    }
});

I very much agree that ReactiveVar should be used as much as possible but I often find myself wishing that I could get a return value when creating child templates that would let me access them…

Does this problem go away using React?