在 emberjs 路由中使用 slug [英] Using a slug in an emberjs route
问题描述
我正试图弄清楚如何在我的 ember 路线中使用 slug(我的模型的属性)来获得更干净的网址.
I'm trying to figure out how to use a slug (attribute of my model) in my ember routes to get cleaner urls.
我希望我的路线如下所示:
I'd like that my routes look like this:
http://www.server.com/#/newsitems/newsitem-title-in-slug-format/1
代替:
http://www.server.com/#/newsitems/1/1
如您所见,我想用实际的 slug 属性替换新闻项目的 id.这是我的 Newsitem
模型的样子:
As you can see, I'd like to replace the id of the newsitem with the actual slug attribute. Here's how my Newsitem
model looks like:
App.Newsitem = DS.Model.extend({
slug: DS.attr('string'),
title: DS.attr('string'),
summary: DS.attr('string'),
});
slug 属性以这种格式接收一个干净的文本属性:title-in-slug-format
The slug property receives a clean text attribute in this format: title-in-slug-format
这是我目前的路由器地图:
This is my router map at the moment:
App.Router.map(function(){
this.resource('newsitems', function(){
this.resource('newsitem', {path:':newsitem_id'});
});
});
我尝试用 newsitem_slug
替换 newsitem_id
但这不起作用.还有其他建议吗?
I tried replacing the newsitem_id
with newsitem_slug
but this isn't working. Any other suggestions?
推荐答案
非常感谢 Michael 为我指明了正确的方向.但是,我认为这是因为我在 rc-1 版本的 ember 中工作,我不必为此覆盖模型挂钩.我唯一要做的就是:
Big thanks to Michael for pointing me in the right direction. But, and I think this is because I'm working in the rc-1 version of ember, I didn't had to override the model hook for this. The only thing I had to do is:
App.NewsitemRoute = Ember.Route.extend({
serialize: function(model, params) {
return { newsitem_id: model.get('slug') };
}
});
这篇关于在 emberjs 路由中使用 slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!