如何访问Template.x.rendered中的iron路由器路径变量

How do I access iron-router path variables in Template.x.rendered?

本文关键字:中的 rendered iron 路由器 变量 路径 Template 何访问 访问      更新时间:2023-09-26

背景

在iron路由器路径定义中,您可以使用变量,如文档中的示例所示:https://github.com/EventedMind/iron-router#controlling-订阅

这一切都很好,它正是我想要的,除了我不想在waitOn子句中这样做,而是在特定子模板的呈现回调中这样做。我有几个这样的,我希望它们独立渲染,而不是像waitOn建议的那样加载。

我的路由器是这样的:

Router.map( function () {
  this.route('de', {
    path: '/monitor/DE/:period',
    data: function () { 
      return {subtitle: "Germany - " + this.params.period + " data"}; 
    }
  });
});

我还有一些模板代码,可以在render上运行以绘制d3图。在这个函数中,我定义了一个包含订阅的Deps.autorun。

Template.de.rendered(function (){
  // lots of d3 stuff...
  // Automatically redraw on change
  Deps.autorun(function () {
    Meteor.subscribe("count", "hourly");
  });
});

我使用如下参数发布id为时间戳的集合:

Meteor.publish("count", function(period) {
  if(period == "hourly") {
    return Count.find({_id: {$gt: new Date().getTime() - 3.6e6*24}}, 
                        {sort: {_id: -1}});
  } else {
    return Count.find({_id: {$gt: new Date().getTime() - 3.6e6*24*30}}, 
                        {sort: {_id: -1}});
  }
});

这段代码运行良好,但我已经在订阅中对参数进行了硬编码。我想使用路径变量来更改订阅范围。

问题

如何使用iron路由器路径变量更改Template.x.rendered回调中的订阅?

您正在寻找:

Router.current().params