admin管理员组

文章数量:1245838

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() {
    // putations 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 repute 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._pute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36) at Tracker.Computation._repute (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 putation when a user navigates away to a new URL/template?
I am using iron:router.

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() {
    // putations 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 repute 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._pute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36) at Tracker.Computation._repute (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 putation when a user navigates away to a new URL/template?
I am using iron:router.

Share Improve this question edited Aug 4, 2015 at 23:10 Kyll 7,1498 gold badges44 silver badges64 bronze badges asked Nov 14, 2014 at 7:36 Jon CursiJon Cursi 3,3714 gold badges28 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

本文标签: javascriptMeteorStop Tracker Autorun when Template is Destroyed (user leaves page)Stack Overflow