javascript - 我如何访问 Template.x.rendered 中的 iron-router 路径变量?

标签 javascript meteor

背景

在 iron-router 路径定义中,您可以使用变量,例如文档中的示例: 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}});
  }
});

这段代码工作正常,但我在订阅中硬编码了参数。我想使用路径变量来更改订阅范围。

问题

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

最佳答案

您正在寻找:

Router.current().params

关于javascript - 我如何访问 Template.x.rendered 中的 iron-router 路径变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24555696/

相关文章:

javascript - jquery .ready 和元素的高度

javascript - Google map API V3 - isLocationOnEdge() 不起作用

javascript - 谷歌地图自动完成功能不改变 map 位置

javascript - Meteor 和 MongoDB 地理空间 - 边界 - $within

javascript - Meteor 方法在客户端返回 null,在客户端运行相同的方法返回正确的值

javascript - 未捕获的语法错误 : Unexpected token < in inline. bundle.js:1 - Angular v4

javascript - 使用 'document' 命名我的变量是否合法?

javascript - 客户端查询延迟 - Meteor 的 Mongo Collections

meteor - Angularfire2: "location.protocol"必须是 http 或 https(一个 Meteor 应用程序)

javascript - 为什么用函数调用替换 Javascript 内联函数会产生不同的行为?