I had an annoying problem loading data asynchronously in the ember route model callback. The problem is that if the model method of my route returns a promise that is rejected, then the route will never try to overestimate this route model. It simply automatically returns the same rejected promises the next time it tries to go on this route without even trying to retrieve the data!
I understand from this answer that the ember route will only call it the model method when trying to convert url to model. I assume that in the case of routes with dynamic segments, it can be called if it had not previously encountered this specific dynamic segment.
This is what I got in the router settings.
window.App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true
});
App.Router.map(function() {
this.route('login');
this.resource('users', { path: '/users' }, function() {
this.resource('user', { path: '/:user_id' });
this.route('create', { path: '/create' });
});
});
And this is my route.
App.UserRoute = Ember.Route.extend({
model: function(params) {
return App.User.fetch(params.user_id);
}
});
I have special error handling in the route of my application, so routes that are not running due to authentication exceptions will redirect the user to the login screen.
App.ApplicationRoute = Ember.Route.extend({
actions: {
sessionExpired: function() {
this.controllerFor('login').set("tokenExpired", true);
this.transitionTo('login');
},
error: function(err) {
if (err.type === "TokenException") {
this.send('sessionExpired');
}
}
}
});
Problem
- I'm going to the route
/users - For some reason, my token expires (inaction, whatever ...)
- I'm going to the route
/users/1 - The route model method returns a promise that is rejected, and they push me onto the login screen.
- I go back and try to go back to the route
/users/1 - The route automatically returns the same as last time, and I went to the login screen.: (
, , - . , , , , .
, , - . ?