How to watch and catch a reactive variable for changes


#1

I have a helper {{ allusers }} which i am using to list all users fetched from my mini mongo. Since allusers helper is reactive, is there a way i can watch and catch allusers so as i can get notified via an event everytime allusers has changed,whether new data was added,data was edited or data was deleted?.


#2

Found it

        var text = Math.random();
        Session.set("text", text);

        this.sessionWatcher = Session.watch('text', function(value) {
        alert('The value of "text" changed! It is now: ' + value);
         });

and this package


#3

Normally you would do that with https://docs.meteor.com/api/collections.html#Mongo-Cursor-observeChanges Working with a Session variable like that looks like a workaround.

What is the exact action you want to take when something changes? Is it rendering some template or is it running some custom code?


#4

I’m with @lucfranken on this. That package is just a thin wrapper around Tracker.autorun and you would normally examine your use case for where you are wanting to track reactive data changes and use the appropriate construct (helpers, template autoruns, Tracker autoruns, etc).