Reputation: 353
Ok, let's say I have a client side function that returns a Session variable, eg:
Template.hello.random = function() {
return Session.get('variable');
};
Somewhere else let's say I have a button that does
Session.set('variable', random_number);
Everytime I hit that button, will my Template.hello.random function run? I find it hard to wrap my head around that..
Upvotes: 1
Views: 210
Reputation: 8865
All the Meteor "Magic" comes from Deps.Dependency
and Deps.Computation
(http://docs.meteor.com/#deps_dependency)
eg
var weather = "sunny";
var weatherDep = new Deps.Dependency;
var getWeather = function () {
weatherDep.depend()
return weather;
};
var setWeather = function (w) {
weather = w;
// (could add logic here to only call changed()
// if the new value is different from the old)
weatherDep.changed();
};
Session
mirror's this pattern, but as a key/value store instead of just one value. (Actually Session
is just an instance of the ReactiveDict
class)
When you have a computation that calls getWeather()
the .depend()
call links it to the computation - and the .changed()
call invalidates
that computation.
eg, getting a computation via Deps.autorun()
computaton = Deps.autorun(function(){
var localWeather = getWeather();
console.log("local weather", localWeather);
});
computation.onInvalidate(function(){
console.log("a dependency of this computation changed");
});
console.log("setting weather");
setWeather("abc");
With this infrastructure - we find out that Template
helpers are run in a computation - and when the dependencies of the computation are .changed()
, it queues up the template to re-render.
Upvotes: 4