https://github.com/gwendall/meteor-reactive-watcher
Watch changes of reactive variables
https://github.com/gwendall/meteor-reactive-watcher
Last synced: 2 months ago
JSON representation
Watch changes of reactive variables
- Host: GitHub
- URL: https://github.com/gwendall/meteor-reactive-watcher
- Owner: gwendall
- Created: 2015-08-18T18:40:39.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-07T14:21:44.000Z (almost 8 years ago)
- Last Synced: 2025-03-18T05:56:57.354Z (3 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
```diff
- NOTE: This package is not maintained anymore.
- If you want to help, please reach out to [email protected]
```Meteor Reactive Watcher
=======================Adds watch methods to reactive variables. Works with ReactiveVar, ReactiveDict - and by extension Session variables.
Inspired by Angular's $watch function.Installation
------------``` sh
meteor add gwendall:reactive-watcher
```Methods
----------### Reactive Var methods
**watch(callback)**
Watch changes of a single variable.### Reactive Dict methods
**watch(key, callback)**
Watch changes of a single variable.**watchGroup(keys, callback)**
Watch changes of any variables in a set.Those functions return a Tracker instance. To stop watching, just call the ```stop()``` method on the returned object.
Example
-------``` javascript
Template.foo.onCreated(function() {/////////////////////////////////
// 1. With a ReactiveVar variable
/////////////////////////////////var reactiveVar = new ReactiveVar(null);
this.reactiveVarWatcher = reactiveVar.watch(function(value) {
console.log('The value of "name" changed! It is now: ' + value);
});//////////////////////////////////
// 2. With a ReactiveDict variable
//////////////////////////////////var reactiveDict = new ReactiveDict(null);
this.reactiveDictWatcher = reactiveDict.watch('name', function(value) {
console.log('The value of "name" changed! It is now: ' + value);
});
this.reactiveDictGroupWatcher = reactiveDict.watchGroup(['name', 'age'], function(values) {
console.log('The value of either "name" or "age" changed! Their new values are now the following.');
for (var key in values) {
console.log(key + ': ' + values[k]);
}
});/////////////////////////////
// 3. With a Session variable
/////////////////////////////this.sessionWatcher = Session.watch('name', function(value) {
console.log('The value of "name" changed! It is now: ' + value);
});
this.sessionGroupWatcher = Session.watchGroup(['name', 'age'], function(values) {
console.log('The value of either "name" or "age" changed! Their new values are now the following.');
for (var key in values) {
console.log(key + ': ' + values[k]);
}
});});
Template.foo.onDestroyed(function() {
this.reactiveVarWatcher.stop();
this.reactiveDictWatcher.stop();
this.reactiveDictGroupWatcher.stop();
this.sessionWatcher.stop();
this.sessionGroupWatcher.stop();
});
```