如何在Template.x.rendered中访问铁路由器路径变量?

时间:2014-07-03 13:56:54

标签: javascript meteor

背景

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

这一切都很好,它完全符合我的要求,除非我不想在waitOn子句中执行此操作,而是在特定子模板的渲染回调中执行此操作。我有几个这样的,我希望它们独立渲染,而不是像waitOn建议的那样加载。

我的路由器看起来像这样:

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

我还有一些模板代码在渲染时运行以绘制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}});
  }
});

此代码工作正常,但我在订阅中对参数进行了硬编码。我想使用path变量来更改订阅范围。

问题

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

1 个答案:

答案 0 :(得分:3)

您正在寻找:

Router.current().params
相关问题