流星检查模板是否存在

Meteor Check if Template Exists

我有使用 Iron-Router 的 Meteor 应用程序,其中呈现的屏幕是基于 URL 的动态屏幕。
我有来自 URL、stationscreen 的两个参数。

路由代码中:
this.render(stationParam+screenParam, {to: 'content'});

但是,我希望能够检查模板 stationParam+screenParam 是否存在。

这可能吗?

所有模板都存储为全局对象的字段,Template。那么问题是:如何检查这个全局对象是否有给定的字段?

您有多种方法可以做到这一点。我个人最喜欢使用 underscore:

if (_.has(Template, stationParam + screenParam)) { /* ... */ }

下划线包含在 Meteor 中。如果您想在包中使用它,请不要忘记 describe 回调中的 api.use('underscore')

另请参阅:Determining if a javascript object has a given property