Ember don't override the model hook


#1

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


#2

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.


#3

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.


#4

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.


#5

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.


#6

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?


#7

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?


#8

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.


#9

Change my linkTo works too:

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

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

#10

Are you all set now?


#11

Yes, it seems like it anyway on my tests.


#12

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

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