MeteorJS - monitor server variable changes and update template value

I have doubts. Not sure if this is possible and did not find a clear answer.

Is it possible to add an “observer” to the server variable, so when the value changes, can I update the view (client side)?

Let's say I have a function var counter = 0and Timeout, which updates the counter variable every minute.

I want to update <span>{{counter}}</span>on the client side. I need to add an “observer” to this server variable and make it reactive.

Thanks in advance!

+4
source share
2 answers

- ( , ).

/:

Counter = new Mongo.Collection("counter");

:

Template.myTemplate.helpers({
  counter: function () {
    return Counter.findOne();
  }
});

, :

if (Counter.find().count() === 0) {
  Counter.insert({value: 0});
}

:

Counter.update({}, {$inc: {value: 1}});

, - _id.

+6

var counter = 0 , . , , Meteor. .

counter , .. Session.set('counter', counter++), . counter

+2

All Articles