0
votes

I'm desperately trying to get slugs in Ember 1.x working.

My serialize method seems to work fine. I can navigate to my route with no problem using the link-to helper or transitionToRoute method. But when I reload my page i get a bunch of errors because my promise resolves in an array of models instead of a single model.

How do I reduce the result of findQuery to one model?

I found a helpful answer for an older Ember.js version here: Using a slug in an emberjs route Unfortunately the solution doesn't work anymore. "one" is not defined, so i tried to adapt my code to the current version. According to the promise documentation, a return value in the then method of the promise object should be passed to the next handler. But I'm still getting an array and my errors afterwards.

My route implementation:

App.ManageRoute = Ember.Route.extend
  model: (params) ->
    promise = @get('store').findQuery('company', {slug: params.company_id})
    promise.then (models) ->
      return models.get("firstObject")
    return promise;

  serialize: (model, params) ->
    return {company_id: model.get('slug')}

[Update]

The solution is to create and return a new Promise object for the single model.

See marvilein's answer

1
Can you show your router mapping plz?mavilein

1 Answers

2
votes

One thing you could do is create a new promise which represents exactly one object instead of the whole array:

App.ManageRoute = Ember.Route.extend
  model: (params) ->
    promise = @get('store').findQuery('company', {slug: params.company_id})
    newPromise = Ember.Deferred.create()
    promise.then (models) ->
      newPromise.resolve(models.get("firstObject"))
    return newPromise;

PS: I don't know coffeescript so there may be minor syntactic errors. And i don't know if Ember Data offers something more convenient to achieve this behaviour :-)