最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor - Stop Tracker Autorun when Template is Destroyed (user leaves page) - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论