在Iron Router中带有默认路由的动态段
Dynamic segments with default routes in Iron Router?
在Meteor中,使用铁路由器,我试图实现一个路由映射,它有一个动态段和一个回退路由,如果动态段不匹配集合中的任何项目。例如,假设我有一个这样的URL:
http://foobar.com/harold我想首先检查harold
是否匹配Posts
集合中的任何id。如果有匹配,那么它应该带我到postPage
模板。
如果没有匹配,那么路由器应该呈现harold
匹配的任何项。
我已经搜索了所有的铁路由器文档,但似乎找不到正确的方法。我想知道是否有像this.next()
这样的东西取消当前的路由映射并进入下一个路由映射。下面是我的尝试:
Router.map(function () {
this.route('postPage', {
// matches: '/MFLS6aKqqRZ2JGz9q'
// matches: '/81zBqGE85aAfjk1js'
path: '/:postId',
before: function () {
//check if segment matches a post ID
post = Posts.findOne(this.params.postId);
if (!post) {
//If no matches, then go to next route
//Something like this.next()?
}
},
data: function() {
return Posts.findOne(this.params.postId);
}
});
this.route('profilePage', {
// matches: '/harold'
// matches: '/brendan'
path: '/:username',
data: function() {
return Profiles.findOne(this.params.username);
}
});
});
你所描述的实际上只是一条路线:/:input
, input
可以是任何东西。正如@David Weldon所提到的,这不是一个好主意;它基本上颠覆了使用路由器的意义,并且每次URL更改时都会导致运行大量额外的代码和数据库查询。
也就是说,如果你真的想这样做,你只需要把所有的代码折叠成一个路由:
Router.map(function () {
this.route('theOneRouteToRuleThemAll', {
path: '/:input',
data: function() {
if (Profiles.findOne({username: this.params.input}) != null)
return Profiles.findOne({username: this.params.input});
else if (Posts.findOne({postId: this.params.input} != null)
return Posts.findOne({postId: this.params.input};
else
return null; // No user or post found; could handle with notFoundTemplate
}
});
});
相关文章:
- 尝试使用Node.js动态路由从IMDB中抓取电影内容.但是在我的output.json文件中没有定义
- 在angularjs中动态路由url路径
- express js中具有不同功能的动态路由
- ASP.NET MVC路由-动态加载.js脚本
- 如何在 html 页面上动态添加路由到脚本 src
- Emberjs:动态模板嵌套和路由
- 如何构造用于骨干路由的动态url
- 动态添加/注册AngularJS控制器,无需路由
- 使用ui路由器实现动态URL路由的最佳方式是什么
- angular ui:ui路由器动态路由和状态
- Slick Carousel在应用程序动态路由的基础上不起作用
- 动态路由PHP&AngularJS
- json动态菜单和路由
- AngularJs 中的动态路由
- AngularJS - 使用 UI-Router 进行动态路由
- 具有动态路由和动态依赖性的角度模块化
- 角度 UI 路由器 - 使用路由传递动态数据
- Expressjs 动态路由问题
- 在 AngularJS 中设置具有动态路由和模板的 12 个产品页面的最佳方式
- AngularJS——基于路由动态加载外部JS