Ember don't override the model hook

According to the docs:

If you’re using Ember Data, you only need to override the model hook if you need to return a model different from the record with the provided ID

But this does not work for me, ember data gives me wrong data.

App.UsersEditRoute = Ember.Route.extend
    model: (params) ->
            return ['Just', 'Some', 'Random']
    setupController: (controller, model) ->
            controller.set('model', model) # Returns @get('store').find 'user', params.user_id

This should return [‘Just’, ‘Some’, Random], but instead it gives me the original @get(‘store’).find ‘user’, params.user_id

Why and how do I get the data I want? Btw, If I do like below, everything works, but I want to know why my model function never is called.

setupController: (controller, model) ->
    controller.set('model', ['Just', 'Some', 'Random']) # returns ['Just', 'Some', 'Random']

Thank you, I’m using ember-data 0.14 and ember 1.0.0

Its a nested route, as such, it only models sometimes. This can be very confusing. Most likely, what’s happening is that EditRoute’s model hook is being called. You test this by putting debugger; code into EditRoute’s model.

Thank you, but how can i override the EditRoute´s model hook? This is really annoying, I do not even have an EditRoute so ember must auto generate it.

It occurred to me that your route’s class name looks strange. It would suggest that you’re nesting Users inside of Edit. Are you doing that?

Can you show your Router.map(function(){?

Also, if you don’t already have Ember Inspector installed then you should install it and look at Routes to make sure that expected class names match your class names.

It feels like I’m doing this right?

App.Router.map (match)->
    @resource 'users', ->
	@route 'new'
	@route 'show', { path: '/:user_id' }
	@route 'edit', { path: '/:user_id/edit' }

I use Ember Inspector and the class name is right, my setupController function is called.

Cool, I should correct myself, your UsersRoute’s model is probably being called instead of UsersEditRoute because edit is nested in UsersRoute. I that correct?

Yeah, that’s right! It have something with dynamic segments to do i think. If i manually go to the url /users/id/edit then it works. So, it is possible to override the model hook when you using nested routes?

Nested routes assume that the data is loaded by the parent route. For example, if you have a profile page with tabs, then the profile model is loaded when the parent is rendered and the nested route uses data from parent.

If you want the model to load every time then you can make the Edit route a resource, like this:

App.Router.map (match)->
    @resource 'users', ->
        @route 'new'
        @route 'show', { path: '/:user_id' }
    @resource 'users.edit', { path: '/users/:user_id/edit' }

Then the model of UsersEditRoute will be loaded every time that this url is rendered.

1 Like

Change my linkTo works too:

// Before  
{{#linkTo 'users.edit' user}}Edit{{/linkTo}}

// After  
{{#linkTo 'users.edit' user.id}}Edit{{/linkTo}}

Are you all set now?

Yes, it seems like it anyway on my tests.

Cool… I’m working on some articles for http://embersherpa.com

Are there any topics that you would be interested in reading?