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

时间:2014-11-06 06:36:41

标签: javascript jquery meteor

我有以下父模板:

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

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

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

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

1 个答案:

答案 0 :(得分:23)

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

相关问题