Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

ember.js - Using a slug in an emberjs route

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

Instead of:

http://www.server.com/#/newsitems/1/1

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'),
});

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'});
  });
});

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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') };
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...