问题
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;
}
And my router.js
file:
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?
回答1:
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;
}
});
回答2:
Finally I've solved this problem: I've followed suggestions and added friendlyTitle
field to object from Articles
collection that contains parsed article title (without special characters).
And this is my route:
this.route('showArticle', {
path: '/:site/:friendlyTitle',
layoutTemplate: 'artLayout',
data: function(){
return Articles.findOne({friendlyTitle: this.params.friendlyTitle});
}
});
:site
field contains name of the object from Sites
collection that has this article assigned (when creating new article I can assing/publish it to/on the specific site).
来源:https://stackoverflow.com/questions/23620192/iron-router-nested-routes-with-multiple-parameters