I’m trying to change my application from sessions to reactive variables, such that the when the page changes the user doesn’t accumulate sessions.
In the application, a session is created for the votes a user made on an item. The items are their own collection and how the user voted on the item is its own collection too. When the items are retrieved, a session of each vote collection is made to produce a reactive frontend (adding classes to the voting buttons). However, instead of using sessions in the template that renders the items, I want to use reactive variables such that it has the same effect as my sessions. I add votes to the template in my body.js template, as below:
function addVotes(element){
var itemId = element._id;
Meteor.call('items.uservotes', itemId, function(error, result) {
if (error) console.log("Error");
Session.set(itemId, result);
});
}
I tried adding reactive variables to my items.js, but the classes were only there when refreshing the template. I used the following in that template:
Template.item.onCreated(function itemOnCreate(){
var self = this;
self.autorun(function(){
self.template = Template.instance();
self.voteVars = new ReactiveVar({});
Meteor.call('items.uservotes', self.template.data._id, function(error, result) {
if (error) console.log("Error");
self.voteVars.set(result);
});
});
});
If you need to the other code to evaluate it, then you can check it out on github. Any other suggestions would be appreciated too.