Creating an advanced timer object
The previous example was a simple demonstration of a custom reactive object. To make it more useful, it is better to create a separate object that hides the Tracker.Dependency
functions and adds additional functionality.
Meteor's reactivity and dependency tracking allows us to create dependencies even when the depend()
function is called from inside another function. This dependency chain allows more complex reactive objects.
In the next example, we will take our timer
object and add a start
and stop
function to it. Additionally, we will also make it possible to choose a time interval at which the timer will rerun:
First, let's remove the previous code examples from the
main.js
andtemplate-helpers.js
files, which we added before, and create a new file namedReactiveTimer.js
insidemy-meteor-blog/client
with the following content:ReactiveTimer = (function () { // Constructor function ReactiveTimer() { this._dependency = new Tracker.Dependency...