Reactivity
From the Meteor docs:
"Meteor embraces the concept of reactive programming. This means that you can write your code in a simple imperative style, and the result will be automatically recalculated whenever data changes that your code depends on."
See above how the teams
helper returns Teams.find()
? And remember how by changing Teams
(creating, updating or removing teams) would result in the page updating automatically? This is reactivity in play.
How to cook a reactive computation
To make something reactive you need three ingredients:
- a reactive data source (like
Teams.find()
) - a function with the reactive data source in it (like the function we passed to the
teams
helper) - a kind of "function upgrader" - something to upgrade the function to be aware that is has to run again when the reactive data source inside it changes (
Template
does this for us above)
There are 7 reactive data sources in Meteor, but we only care about the top 2 at the moment:
- Database queries on collections (like
Teams.find()
) - Session variables (we will use these in a second)
Meteor.status
- The
ready()
method on a subscription handle Meteor.user
Meteor.userId
Meteor.loggingIn
There are 3 "function upgraders" in Meteor. I'll list them all, but we only care about the top one:
- Templates
Tracker.autorun
Blaze.render
andBlaze.renderWithData
So without really understanding it, we have already cooked up some reactivity already by listing our teams. But now we want to use it again for our isCreatingTeam
helper. To do this we'll utilize the Session
reactive data source.