Assuming i’m currently in route ‘foo’, it sometimes make sense to requesting a model re-load for the same transition, but invoking TransitionToRoute(‘foo’) would not re-load the model due to:
Note that for routes with dynamic segments, this hook is only
executed when entered via the URL. If the route is entered
through a transition (e.g. when using the `linkTo` Handlebars
helper), then a model context is already provided and this hook
is not called. Routes without dynamic segments will always
execute the model hook.
But in situations such as performing some server invocation that changes too many properties in the model, I’d like to reload the model,
Currently I maintain dirty workarounds to achieve that but I’d like to have a better approach,
Does it make sense to extend the transition mechanism to allow model re-load even though the transition context already contains a model? or is there a better approach?
I’m pretty sure that afterModel should do what you need here:
afterModel: (model) ->
model.reload()
I’m pretty sure that afterModel will run when using transitionToRoute (tested with v1.5.1), and if afterModel it returns a promise, the transition will wait for the promise to fulfill.
I have come back to this topic a few times now trying to find a solution for this stuff. I was using setupController but that changes some of the nice things model gives you like waiting on the page to load.
So my solution from this topic…
import Ember from 'ember';
var modelHookRun;
export default Ember.Route.extend({
beforeModel() {
modelHookRun = false;
},
model(params) {
modelHookRun = true;
return this.store.findRecord('topic', params.id, {reload: true});
},
afterModel(model) {
if ( ! modelHookRun) {
return model.reload();
}
},
});