Building a simple reactive object
As we saw, a
reactive object is an object that when used inside a reactive function, will rerun the function when its value changes. The Meteor's Session
object is one example of a reactive object.
In this chapter, we will build a simple reactive object that will rerun our {{formatTime}}
template helper at time intervals so that all the relative times are updated correctly.
Meteor's reactivity is made possible through the Tracker
package. This package is the core of all reactivity and allows us to track dependencies and rerun these whenever we want.
Perform the following steps to build a simple reactive object:
To get started, let's add the following code to the
my-meteor-blog/main.js
file:if(Meteor.isClient) { ReactiveTimer = new Tracker.Dependency; }
This will create a variable named
ReactiveTimer
on the client with a new instance ofTracker.Dependency
.Below the
ReactiveTimer
variable, but still inside theif(Meteor.isClient)
condition, we will add the...