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:

  1. a reactive data source (like Teams.find())
  2. a function with the reactive data source in it (like the function we passed to the teams helper)
  3. 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)

Ingredients of Reactivity\label

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 and Blaze.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.