Book Image

Building Single-page Web Apps with Meteor

By : Fabian Vogelsteller
Book Image

Building Single-page Web Apps with Meteor

By: Fabian Vogelsteller

Overview of this book

Table of Contents (21 chapters)
Building Single-page Web Apps with Meteor
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. 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 of Tracker.Dependency.

  2. Below the ReactiveTimer variable, but still inside the if(Meteor.isClient) condition, we will add the...