【问题标题】:Meteor - how to tell what's causing a template to re-render?Meteor - 如何判断是什么导致模板重新渲染?
【发布时间】:2014-02-10 08:28:31
【问题描述】:

我有一个 Meteor 模板,它在最初的几秒钟内重新渲染了大约 6 次,使微调器 (spin.js) 卡顿。

我不知道为什么模板本身会重新渲染,并且添加 {{#isolate}} 和/或 {{#constant}} 标记没有帮助。

我的问题:是否有一种编程方式来确定触发给定重新呈现的事件或反应数据源?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    因此,要确定哪个模板助手失效,您有几个选择:

    1. 首先是编辑 Meteor packages/deps/deps.js 的源代码,然后将堆栈跟踪添加到 invalidate 函数。

      您可以使用 try{throw new Error('');}catch(ex){ console.log(ex.stack);} 之类的内容获取堆栈跟踪

    2. 代理Deps.Computation.prototype.invalidate,类似于(1)

      var oldInvalidate = Deps.Computation.prototype.invalidate;
      Deps.Computation.prototype.invalidate = function(){
        try{throw new Error('');}catch(ex){ console.log(ex.stack);}
        return oldInvalidate.apply(this,arguments);
      }
      
    3. 创建一个响应式函数包装器,它在失效时打印一条日志消息

      您可以执行类似于 isolate-value 包的操作 - 但添加一个日志语句(并删除值隔离)

      (免责声明:尚未完全测试此代码,但您可以大致了解)

        logInvalidation = (stmt, fn) ->
          value = null
          outerComputation = Deps.currentComputation
          dep = new Deps.Dependency()
          c = Deps.autorun (c) -> 
            # kill this computation if the computation wrapping the handler is stopped
            if outerComputation?.stopped
              c.stop()
              return
            unless c.firstRun
              # a dependency from `fn` was changed (hence this computation was invalidated)
              # so, invalidate anything that depends on `dep`
              console.log("Helper invalidated #{stmt}")
              dep.changed()
            else
              # attach dependencies in `fn`, and pass result to `value`
              value = fn()
            return
          dep.depend()
          return value
      

      然后像这样包装你的可疑助手:

        Template.myTemplate.helpers
          myHelper: ()->
            return logInvalidation 'myHelper invalidated!', ()->
              return Session.get("myValue")
      

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多