在Meteor中访问模板渲染函数中的父数据上下文

时间:2021-10-26 16:35:31

I have the following parent template:

我有以下父模板:

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

I want to access the parent data context in childTempl:

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

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

How can I do this? Any help would be greatly appreciated.

我怎样才能做到这一点?任何帮助将不胜感激。

1 个解决方案

#1


23  

You can use Template.parentData(n) to access the parent context inside any template helper or rendered callback. See the docs here. Internally, all it does is call the Blaze getView method for the parent view until it hits the desired parent context (as defined by n).

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

#1


23  

You can use Template.parentData(n) to access the parent context inside any template helper or rendered callback. See the docs here. Internally, all it does is call the Blaze getView method for the parent view until it hits the desired parent context (as defined by n).

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