11

In my Meteor template, I have a function called ohlcInit() that is autorun when new data is available in Mongo:

Template.Live.rendered = function(){

  function ohlcInit() {
    // computations run here
  }

  Tracker.autorun(function() {
      ohlcInit();
  });
};

This works great while the user is on the page/template in which this is all defined, but as soon as the user navigates to another URL on the site and the template is destroyed, errors get thrown in the console:

Exception from Tracker recompute function: undefined is not a function TypeError: undefined is not a function at ohlcInit (http://localhost:3000/client/views/live/live.js?dd5fb618daf9ea9e233c37caaaa9ed200fe3e987:271:33) at http://localhost:3000/client/views/live/live.js?dd5fb618daf9ea9e233c37caaaa9ed200fe3e987:306:5 at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36) at Tracker.Computation._recompute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:302:14) at Tracker.flush (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:430:14)

How do you safely stop/end the autorun computation when a user navigates away to a new URL/template?
I am using iron:router.

Kyll
  • 7,036
  • 7
  • 41
  • 64
Jon Cursi
  • 3,301
  • 4
  • 27
  • 53

1 Answers1

16

Use the new Template.autorun function, which automatically cleans itself up after the template is destroyed. To use it inside of a rendered callback, just replace Tracker.autorun with this.autorun.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Thanks! Just saw this same explanation in the Meteor github repo: https://github.com/meteor/meteor/issues/2057 – Jon Cursi Nov 14 '14 at 07:49
  • I'm running into the same above issue but I'm starting my `Tracker.autorun` calls in the callback of a Meteor method on the client. I'm starting a process on the server and then need to listen to some database values if there was no error in starting the process. They work fine except I get the same above error when destroying the template. Using `this.autorun` doesn't work in the callback scope. I assume there's no way to call stop in the `.destroyed` because of scope issues. – evolross Jul 25 '15 at 03:06
  • 1
    I figured it out. Just needed to set a `var template = Template.instance()` at the correct scope **before** my Meteor method call. Then in the callback code call `template.autorun(function ...` referring to the above variable works perfect. – evolross Jul 25 '15 at 21:42
  • hello @DavidWeldon,,can you give simple example..?? i've read the docs but don't understand,, –  Dec 03 '15 at 16:41
  • 1
    @KarinaL have a look at my answers to [here](https://stackoverflow.com/questions/29418433/is-there-any-way-to-insert-a-callback-before-after-a-templates-context-is-update) and [here](https://stackoverflow.com/questions/29907410/how-do-i-get-onrendered-to-run-a-second-time-when-the-route-changes-but-the-temp). Hopefully those will help. If you have some specific code that you can't get working, I'd recommend asking a new question. – David Weldon Dec 04 '15 at 05:01