模板更新的流星反应函数 [英] Meteor reactive function for Template updates

查看:40
本文介绍了模板更新的流星反应函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Meteor.js 中解决以下问题:

I want to solve the following problem in Meteor.js:

我有一个 HTML 元素,仅当用户登录时才会出现在模板中:{{#if currentUser}} <输入...>{{/if}}.在 HTML 元素出现后,我需要对其执行 JS 命令.因此,我需要在模板更新后 发生某种回调.如何实现这一目标?

I have a HTML element that appears in a template only if the user is logged in: {{#if currentUser}} <input ...> {{/if}}. After the HTML element appears I need to execute a JS command on it. Thus I need some kind of callback that happens after the Template is updated. How to achieve this?

在这种情况下,我所知道的所有反应性数据源都没有做到这一点,例如 Meteor.user() 源在更新模板之前触发依赖项.另请参阅我之前的问题此处.

None of the reactive data sources I know of do the trick in this case as for example the Meteor.user() source triggers dependencies before the Template is updated. Also see my prior question here.

推荐答案

重构你的模板代码,将 {{#if}} 块助手的内容放在子模板中,这将得到它自己的 onRendered 生命周期事件仅在满足两个条件时触发,并且隐含的新 HTML 内容插入到 DOM 中.

Refactor your template code to put the content of the {{#if}} block helper inside a child template, that will get it's own onRendered life cycle event triggered only when both the condition is met, and the new HTML content implied is inserted in the DOM.

HTML

<template name="parentTemplate>
  {{#if currentUser}}
    {{> childTemplate}}
  {{/if}}
</template>

<template name="childTemplate">
  {{! whatever}}
</template>

JS

Template.childTemplate.onRendered(function(){
  // here you go
});

这篇关于模板更新的流星反应函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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