Using bullets in emberjs route

I am trying to figure out how to use slug (an attribute of my model) in my ember routes to get cleaner urls.

I would like my routes to look like this:

http://www.server.com/#/newsitems/newsitem-title-in-slug-format/1 

Instead:

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

As you can see, I would like to replace the newsitem identifier with the actual slug attribute. Here's what 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 gets a pure text attribute in this format: title-in-slug-format

This is the map of my router at the moment:

 App.Router.map(function(){ this.resource('newsitems', function(){ this.resource('newsitem', {path:':newsitem_id'}); }); }); 

I tried replacing newsitem_id with newsitem_slug , but this does not work. Any other suggestions?

+6
source share
2 answers

Many thanks to Michael for pointing me in the right direction. But, I think this is because I work in the rc-1 version of ember, I did not have to redefine the model hook for this. The only thing I needed to do:

 App.NewsitemRoute = Ember.Route.extend({ serialize: function(model, params) { return { newsitem_id: model.get('slug') }; } }); 
+8
source

To do this, you need to follow a few steps. First, you need to configure your route to override the model hook:

 App.NewsitemRoute = Ember.Route.extend({ model: function(params) { console.log("Searching for newsitem with slug: ", params.newsitem_id); var newsitems = App.Newsitem.findQuery({ slug: params.newsitem_id }); newsitems.one("didLoad", function() { newsitems.resolve(newsitems.get("firstObject")); }); return newsitems; } }); 

In the above example, we request App.Newsitem for records matching the specified pool. This is more complicated than a simple id-based search, since we need to translate the array of query results into one record. For more on how this works, see: how-to-find-a-model-by-any-attribute-in-ember-dot-js

You also need to implement serialize so that ember linkTo can correctly create links.

+2
source

All Articles