在emberjs路线中使用s子 [英] Using a slug in an emberjs route

查看:95
本文介绍了在emberjs路线中使用s子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在我的余烬路线中使用s lug(我的模型的属性)来获得更干净的网址。

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属性替换newsitem的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_id 替换为code> newsitem_slug 但这不工作。任何其他建议?

I tried replacing the newsitem_id with newsitem_slug but this isn't working. Any other suggestions?

推荐答案

非常感谢迈克尔指出我的方向正确。但是,我认为这是因为我正在使用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路线中使用s子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆