【发布时间】: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