【问题标题】:How to get the "root" data in a Blaze template or template helper如何在 Blaze 模板或模板助手中获取“根”数据
【发布时间】:2015-04-18 03:12:32
【问题描述】:

假设我必须包含相同嵌套模板的页面:

第1页:

>rootTeplate
  >fooTemplate
    >barTemplate

第2页:

>rootTeplate
  >barTemplate

如何编写一个模板助手,让我可以在 barTemplate 中访问 rootTemplate 的数据?我知道我可以使用 ../ 访问父上下文,但在这种情况下,barTemplate 不知道它必须返回多少级别。

【问题讨论】:

    标签: templates meteor meteor-blaze


    【解决方案1】:

    您需要确定要返回多少级。在barTemplate 的助手中,您可以使用Template.parentData

    Template.barTemplate.helpers({
         something: function() {
             var parentDatas = Template.parentData()
    
             //If there are two levels
             var parentDatas = Template.parentData(2)
         }
    });
    

    您需要确定要访问多少级别,然后可以将此号码插入Template.parentData

    如果您不确定需要返回多少级别,则可能不值得使用此范例。考虑使用来自路由器的data 上下文或Session 哈希。

    【讨论】:

    【解决方案2】:

    在这种情况下,您可以使用 Router.current().data() 从 Iron:router 访问数据上下文。

    Template.barTemplate.helpers({
      var data = Router.current() && Router.current().data();
      if(data)
        // use it
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2017-08-20
      • 2014-05-12
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多