【问题标题】:How do I access iron-router path variables in Template.x.rendered?如何访问 Template.x.rendered 中的 Iron-router 路径变量?
【发布时间】:2014-07-03 13:56:53
【问题描述】:

背景

在 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 回调中的订阅?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    您正在寻找:

    Router.current().params
    

    【讨论】:

    • 天哪。我之前在控制台中检查过,但后来它不起作用。将其添加到订阅解决了它!谢谢!
    • 没问题。很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2015-10-17
    • 2018-03-20
    相关资源
    最近更新 更多