【问题标题】:Meteor OnRendered function after child Template子模板后的 Meteor OnRendered 函数
【发布时间】:2016-01-05 19:31:28
【问题描述】:

父模板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");
});

【问题讨论】:

    标签: javascript jquery meteor meteor-blaze


    【解决方案1】:

    虽然使用Tracker.afterFlush 是产生所需行为的选项,但执行此类操作的最佳方法是仅使用子模板的onRendered 函数。渲染子模板后,将执行所需的代码。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 2016-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        相关资源
        最近更新 更多