javascript 在 Meteor 的模板渲染函数中访问父数据上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26773161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access parent data context in Template rendered function in Meteor
提问by user3475602
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.
我怎样才能做到这一点?任何帮助将不胜感激。
回答by mark
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 定义)。