Meteorjs:如何知道模板是否被渲染? [英] Meteorjs: How to know if template is re rendered?

查看:170
本文介绍了Meteorjs:如何知道模板是否被渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

Template.temp.rendered = function () {
    console.log('temp rendered');
}

只有在初始化网站时才会记录。

Which logs only when the website is initialized.

但是我做这样的事情:

more = function () {
    Meteor.subscribe('Videos', Session.get('more_info'));
}

当我打电话给();即使模板dom更新为新文档,它也不会记录临时呈现。
还尝试了一些类似的东西:

When i call more(); it does not log the "temp rendered" even though the template dom gets updated with the new documents. Also tried something like:

Template.temp.rerendered = function () {
    console.log('temp re rendered');
}

它不工作;
如何知道A模板是否重新上传?

It does not work; How do I know if A template is rerendered ?

目前我正在做这样的事情

For the moment i'm doing something like this

$('#list').bind("DOMSubtreeModified",function(){
    console.log('template modified'}
    //this logs like 200 times, every time the template is re rendered
)

我如何以流星的方式执行?

How can I do it in the meteor way ?

列表模板:

<template name="list">
    {{#each list}}
        <a id="{{_id}}" href="/{{category}}/{{_id}}" title="{{vTitle}}">
            {{vTitle}}
        </a>
    {{/each}}
</template>

助手:

Template.list.helpers({
  list : function () {
     return Videos.find({},{sort : {date : -1}});
  }
})

尝试(不工作):

Template.list.rendered = function () {    
  this.autorun(function() {
    Videos.find();
    console.log('template re rendered');
  });
}

优先解决方案(来自@richsilv):

Prefered Solution(from @richsilv):

Template.list.rendered = function () {    
  this.autorun(function() {
    Videos.find().count();
    console.log('template re rendered');
  });
}

另外,@Peppe LG的解决方案如果您需要调用函数每次渲染模板,如果你不想注册一个自动运行。

Also the solution from @Peppe L-G was good if you need to call a function every time the template is rendered and if you dont want to register an autorun.

推荐答案

是的, c>回调是一次 当模板最初呈现时,而不是当它由于依赖于它的无效计算而变化时。

Yes, the rendered callback is only fired once when the template is originally rendered, not when it changes due to computations on which it depends being invalidated.

Meteoric的做法是添加一个 this.autorun 到呈现的回调,即取决于导致模板重新投递的相同内容(例如视频集合中找到或其他任何内容)。这样你就是:

The Meteoric way to do things is to add a this.autorun to the rendered callback which is dependent on the same thing that's causing the template to rerender (i.e. a find on the Videos collection, or whatever). That way you are:


  1. 将您的反应性与数据源相比,而不是DOM,除了从概念的角度看,也意味着每当你的模板发生任何变化时,你不会运行不必要的计算,这是非常重要的,如果它依赖于多个数据源(你可以有一个不同的 autorun 每个来源,必要时)。

  2. 仍然利用模板的能力停止 autorun 这个下,当它被拆除时,这样可以避免需要手动停止它们,以避免有大量浮动,未使用的自动运行程序占用CPU和内存。

  1. Tying your reactivity to the data source rather than the DOM which, apart from being more sensible from a conceptual perspective, also means you don't run a load of unnecessary calculations every time there is any change to your template, which is very important if it is dependent on multiple data sources (you can have a different autorun for each source, where necessary).
  2. Still taking advantage of a template's ability to stop autorun blocks which are declared under this when it's torn down, which obviates the need to stop them manually to avoid having loads of floating, unused autoruns taking up CPU and memory.

这篇关于Meteorjs:如何知道模板是否被渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆