首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确阻止流星Tracker.autorun?

如何正确阻止流星Tracker.autorun?
EN

Stack Overflow用户
提问于 2014-10-12 09:25:17
回答 1查看 3.7K关注 0票数 3

我有以下几点:

代码语言:javascript
复制
Meteor.startup(function() {
  var computation = Tracker.autorun(function() {
    var currentChapter;
    currentChapter = Chapters.findOne({
      _id: currentChapterId
    });
    if (currentChapter) {
      if (currentChapter.title) {
        $("#input-title").val(currentChapter.title);
      } else {
        $("#input-title").val("");
      }
      if (currentChapter.content) {
        $("#input-content").html(currentChapter.content);
      } else {
        $("#input-content").html("");
      }
    }
    return computation.stop();
  });
});

现在我明白了:

跟踪器afterFlush函数的异常:无法调用未定义的TypeError的方法“停止”:无法调用未定义的方法“停止”

我想要做的是,一旦currentChapter为真,就停止计算。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-12 10:24:23

有两件事:

1-您的自动运行函数获得传递给它的计算句柄,因此您可以像这样停止它:

代码语言:javascript
复制
Meteor.startup(function() {
  var computation = Tracker.autorun(function(thisComp) {
    var currentChapter;
    currentChapter = Chapters.findOne({
      _id: currentChapterId
    });
    if (currentChapter) {
      if (currentChapter.title) {
        $("#input-title").val(currentChapter.title);
      } else {
        $("#input-title").val("");
      }
      if (currentChapter.content) {
        $("#input-content").html(currentChapter.content);
      } else {
        $("#input-content").html("");
      }
      thisComp.stop();
    }
  });
});

2-在您的代码中,计算将在第一次运行结束时停止,不管如何-您应该在if (currentChapter)块中停止它。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26323633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档