问题描述
我有两个模型:网站
和文章
。每个站点可以有多篇文章。我想按以下方式查看文章: / siteName / articleFriendlyTitleUrl
。目前,我有帮助方法来制作如下所示的友好网址:
I have two models: sites
and articles
. Each site can have multiple articles. I would like to view the article in the route like this: /siteName/articleFriendlyTitleUrl
. At the moment I have helper method to make friendly urls like this:
getFriendlyUrl = function(urlString){
urlString = urlString.toLowerCase();
urlString = str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-');
return urlString;
}
和我的 router.js
文件:
this.route('showArticle', {
path: '/article/:title'),
layoutTemplate: 'artLayout',
data: function(){
viewData = {
title: Articles.findOne({title: this.params.title}),
site: Sites.findOne({name: this.name})
}
return viewData;
});
任何人都知道如何实现此目标吗?我也尝试过实现此路径:
Anyone have any idea how to implement this? I've also tried achieve this path:
/:siteName/:articleId
但没有成功-有任何建议吗?
but without success - any suggestions?
推荐答案
我建议将条塞保存在文章文档中。
应用程序可以使用Underscore.String.slugify函数(),当用户添加/编辑文章时。
I recommend to save slug inside article document.App can generate slug from title using Underscore.String.slugify function (https://github.com/epeli/underscore.string) when user add/edit article.
具有多个参数的路由有效:
Route with multiple params are valid:
this.route('showArticle', {
path: '/:siteName/:articleSlug'),
...
data: function(){
viewData = {
title: Articles.findOne({slug: this.params.articleSlug}).title,
site: Sites.findOne({name: this.params.siteName})
}
return viewData;
}
});
这篇关于铁路由器嵌套的具有多个参数的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!