Timmie Sarjanen
Timmie Sarjanen

Reputation: 411

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

Upvotes: 0

Views: 779

Answers (2)

redpsalm
redpsalm

Reputation: 1

I had a similar problem when I wanted to override the model hook. The answer from Simon gave me the right direction. In addition, it should be noted, also from the ember guide but in the Links section, that the {{link-to}} helper takes:

At most one model for each dynamic segment. By default, Ember.js will replace each segment with the value of the corresponding object's id property. If there is no model to pass to the helper, you can provide an explicit identifier value instead. The value will be filled into the dynamic segment of the route, and will make sure that the model hook is triggered.

So the bottom line is that by replacing the model in the {{link-to}} helper (in my case 'product') by the object id (in my case 'product.id'), my model hook is now called every time.

Upvotes: 0

Simon Cateau
Simon Cateau

Reputation: 651

The model hook, for routes with a dynamic segment, is only called when the page is (re)loaded, here's what the ember guide says (the note at the end):

Note: A route with a dynamic segment will only have its model hook called when it is entered via the URL. If the route is entered through a transition (e.g. when using the link-to Handlebars helper), then a model context is already provided and the hook is not executed. Routes without dynamic segments will always execute the model hook.

Upvotes: 0

Related Questions