I have a problem with updating some values in Meteor app on client-side. I’m trying to understand how ReactiveVar works.
When I use find() method on collection on client-side the site updates immediately each time I change something. I want to achieve the same effect using ReactiveVar and server-side Method. So the code below works correctly for me:
// Client Template.body.onCreated(function appBodyOnCreated() { this.subscribe('activities'); } Template.body.helpers({ getCounter() { return Activities.find({ editorId: Meteor.userId(), 'referredObject.type': 'LIST' }).count(); } });
But when I try to achieve the same effect with server-side Method it doesn’t work correctly. Code below updates variable only once. If I want to get current value I need to refresh the page.
// Server Meteor.methods({'activitiesCreateCount'(userId, objectType) { check(userId, String); check(objectType, String); return Activities.find({ editorId: userId, 'referredObject.type': objectType }).count(); } });
// Client
Template.body.onCreated(function appBodyOnCreated() { this.subscribe('activities'); this.activitiesAmount = new ReactiveVar(false); }
Template.body.helpers({ getCounter() { var tempInstance = Template.instance(); Meteor.call('activitiesCreateCount', Meteor.userId(), 'TODO', function(err, response) { tempInstance.activitiesAmount.set(response); }); return Template.instance().activitiesAmount.get(); } });
How I can improve my code if I want always have a current value of the variable (like in the first client-side only example)?