访问Meteor中Template渲染函数中的父数据上下文

Access parent data context in Template rendered function in Meteor

本文关键字:数据 函数 上下文 Meteor Template 访问      更新时间:2023-09-26

我有以下父模板:

<template name="parentTempl">
    {{#each child}}
       {{> childTempl}}
    {{/each}}
</template>

我想访问childTempl:中的父数据上下文

Template.childTempl.rendered = function() {
    console.log(this.parent.data); // ?
};

我该怎么做?如有任何帮助,我们将不胜感激。

您可以使用Template.parentData(n)访问任何模板助手或呈现回调中的父上下文。请参阅此处的文档。在内部,它所做的只是为父视图调用Blaze getView方法,直到它达到所需的父上下文(如n所定义)。