Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 508
polyclick Avatar asked Feb 18 '13 22:02

polyclick


1 Answers

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') };
  }
});
like image 61
polyclick Avatar answered Sep 28 '22 09:09

polyclick