observeChanges questions

Hi all,

Some questions about “observeChanges”. If “observeChanges” is not in helpers, does it need Tracker.autorun?

I test it outside onCreated, helpers. It seems like it doesn’t need Tracker.autorun to be reactive. Is it a proper way to use it in such way?

Please advice, thank you.

observeChanges is reactive by itself. You don’t even need to use in a template helper, you can use anywhere. All it does is call your hook functions when the data changes.

In fact, to be more precise, it is not a reactive source for Tracker. The cursor that triggers it (i.e. the ‘find’) is what is a reactive source. Reactive sources are cursors, sessions and reactive vars (and any derivative of these)

2 Likes

Thanks for the clarification. Appreciate.

When will it be cancel? If I define it outside of any template, is it keep on running no matter which page/route I’m in?

Look in the docs, it returns an object, call stop on that:

var observer = collection.find({}).observeChanges({onChanged: ...});
observers.stop();

Ya so I guess it will keep on running no matter where I navigate. Anyway, thanks.