繁体   English   中英

子模板后的Meteor OnRendered功能

[英]Meteor OnRendered function after child Template

父模板的onRendered函数在子模板之前调用。 子模板渲染后如何执行父模板功能。

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

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

现在我需要执行一些文档准备功能

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

您可能需要将autorunafterFlush结合使用。 尝试以下操作:

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');
      });
    }
  });
});

使用Tracker.afterFlush是产生所需行为的选项时,执行类似操作的最佳方法是仅使用子模板的onRendered函数。 呈现子模板后,将执行所需的代码。

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

这种方法更加自然,并且允许子模板也可以在任何其他模板中使用,而不会“破坏”

暂无
暂无

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

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