Route#model hook not being called on transitions is problematic

When editing a model, it may be desirable to be able to force reload the model either to ensure it’s the latest revision, or to return additional attributes necessary for the edit operation. Transitioning to edit a loaded model does not trigger the model hook per the docs, and it’s unclear as to why.

Given an index of items, removing the model from the store to force a reload will cause the item to disappear from the index before the transition is complete, for an odd UX. Is there a strong reason not to call the model hook with an arguments (or reference to existing model if present) or an alternate hook?

There’s a new method getting added as part of the query params feature going into beta this weekend called Route.refresh(). I believe this does what you’re asking.

/**
Refresh the model on this route and any child routes, firing the
`beforeModel`, `model`, and `afterModel` hooks in a similar fashion
to how routes are entered when transitioning in from other route.
The current route params (e.g. `article_id`) will be passed in
to the respective model hooks, and if a different model is returned,
`setupController` and associated route hooks will re-fire as well.

An example usage of this method is re-querying the server for the
latest information using the same parameters as when the route
was first entered.

Note that this will cause `model` hooks to fire even on routes
that were provided a model object when the route was initially
entered.

@method refresh
@return {Transition} the transition object associated with this
  attempted transition
@since 1.4.0
*/
1 Like

Awesome, thanks for replying!

It’s finally actually shipping?!?

HOORAY! You should have a party!

Fantastic. Will this also fire setupController? I’m running to a problem where I need the route to act like it was newly transitioned to even if all I’m really doing is bubbling back up the route tree. This may actually be overkill for my problem, however, because I don’t really need or want the model hook to fire, just setupController.

Sorry if this is a dumb question, but I’m a beginner with Ember:

Could somebody provide an example of where this .refresh() would be placed? I have an application in which I need to reload a model from the server on every single route, and the way I found to accomplish this was to call .reload() on the model all the time, but this .refresh() seems cleaner. The only problem is that every solution I’ve tried with this made me run into an infinite loop since all of the route hooks get called again when you run this.

Thank you!

I’m not sure if refresh is cleaner but it will not work in model hooks as you describe above. I think reload (on the model object, I suppose) is fine.

On the longer term, model hooks will be moved to the routable component for the route and will always fire, which might solve your problem.