【问题标题】:Access parent data context in Template rendered function in Meteor在 Meteor 的模板渲染函数中访问父数据上下文
【发布时间】:2015-01-02 14:05:29
【问题描述】:

我有以下父模板:

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

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

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

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

【问题讨论】:

  • 是的。问题是,当我在 rendered 函数中使用 Template.parentData(1)._id 时,我收到此错误:Error: There is no current view
  • 这很奇怪,我刚刚测试过它似乎可以工作。 this.view 或 Blaze.currentView 在渲染函数中返回什么?
  • 对不起。它工作正常,我把Template.parentData(1)._id 放在错误的上下文中。非常感谢!
  • 太棒了! :) 为了完整起见,我将把它作为一个答案。

标签: javascript jquery meteor


【解决方案1】:

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

【讨论】:

  • 在活动中如何做到这一点? Template.hello.events()?
猜你喜欢
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 2015-05-08
  • 2014-10-10
  • 2015-05-06
  • 2014-12-09
  • 2015-01-04
  • 2015-10-12
相关资源
最近更新 更多