1
votes

I've got an ember-data model that has an id as well as a server-generated custom slug attribute (which is prefixed with the id if that matters).

I'd like to use the slug instead of the id in all public routres. For that purpose, I have changed the router:

App.Router.map ->
  @resource 'strategies', path: '/strategies', ->
    @resource 'strategy', path: ':strategy_slug'

and overwrote the serialize method of the respective route:

App.StrategyRoute = Ember.Route.extend()
  serialize: (model) ->
    {
      strategy_slug: model.get('slug')
    }

Unfortunately, this does not seem to work when using transitionToRoute from a controller to transition to, e.g., /strategies/123-test:

App.ExampleController = Ember.ObjectController.extend()

  [...]

  actions:
    showDetails: ->
      @transitionToRoute("/strategies/#{@get('slug')}")
      false

(where @get('slug') returns '123-test')

All output I get in the console after invoking the showDetails action is:

Transitioned into 'strategies.index'

Ember does seem to recognize the slug-based route.

Ember: 1.5.0-beta.2+pre.3ce8f9ac
Ember Data: 1.0.0-beta.6

Is there anything I may have missed?

Edit:

The following variant works and is feasible in this case, but I have another use-case where I have only access to the URL.

App.ExampleController = Ember.ObjectController.extend()

  [...]

  actions:
    showDetails: ->
      @transitionToRoute('strategy', @get('content'))
      false
1

1 Answers

0
votes

I eventually figured out how to solve this. The key is to add a model method to the router, which knows how to turn a slug into a model:

App.StrategyRoute = Ember.Route.extend()
  model: (params, transition) ->
    App.store.getById('strategy', parseInt(params.strategy_slug))

  serialize: (model) ->
    {
      strategy_slug: model.get('slug')
    }