Making a custom library reactive
Because we so often deal with variables and data, it can go unnoticed that Meteor's reactivity doesn't only work with reactive values. Any function from any JavaScript library can be turned into a reactive provider. This recipe will show you how to create your own reactive providers using the Tracker.depend()
and Tracker.changed()
commands.
Getting ready
To keep the example simple, we will use a default Meteor project, with a bootstrap
package, and a random color generator. Open a terminal window, navigate to where you would like to create your root project, and execute the following commands:
$ meteor create customreactive $ cd customreactive $ meteor add twbs:bootstrap $ meteor add rzymek:randomcolor $ meteor
How to do it…
Let's pretend you have a (pretty awesome) library called colorsaurus
. Your colorsaurus
object likes to roar. A lot. Mostly because "rawr" means "I love you" in dinosaur, but also because the colorsaurus wants to share as many random colors...