简体   繁体   English

子模板后的Meteor OnRendered功能

[英]Meteor OnRendered function after child Template

Parent template onRendered function invokes before child template. 父模板的onRendered函数在子模板之前调用。 How to execute parent templates function after child template rendered. 子模板渲染后如何执行父模板功能。

<template name="parent">
 {{#each object}}
  {{> child}}
 {{/each}}
</template>

<template name="child">
 <img src="someurl" data-src="someurl">
</template>

Now I need to execute some document ready function so 现在我需要执行一些文档准备功能

Template.parent.onRendered(function() { // doesnt invokes after child template
 $("img").unveil();
 $(window).trigger("lookup");
});

A combination of autorun and afterFlush is probably what you need. 您可能需要将autorunafterFlush结合使用。 Give something like this a try: 尝试以下操作:

Template.parent.onRendered(function() {
  this.autorun(function() {
    // replace the find with whatever is in your helper
    // which returns the children array/cursor
    if (Children.find().count()) {
      // this should run after the child templates have been rerendered
      Tracker.afterFlush(function() {
        $('img').unveil();
        $(window).trigger('lookup');
      });
    }
  });
});

While using Tracker.afterFlush is an option that produces the needed behavior, the best way to do something like this is to just make use of the child template's onRendered function. 使用Tracker.afterFlush是产生所需行为的选项时,执行类似操作的最佳方法是仅使用子模板的onRendered函数。 As soon as the child template is rendered then needed code will execute. 呈现子模板后,将执行所需的代码。

Template.child.onRendered(function() {
  this.$('img').unveil();
  this.$(window).trigger('lookup');
});

This approach is more natural and allows the child template to be used in any other template as well without "breaking" 这种方法更加自然,并且允许子模板也可以在任何其他模板中使用,而不会“破坏”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM