Reputation: 6229
I am using Ember 1.0.0 pre and use the REST Adapter to fetch an object from a Rails API. I render a template if the object is found via the REST API and set the found model object as the model of the view. In order to do that, I use the model hook in the Route and everything seems to work fine. Naturally, I want to render some special 404 template/view if the model object is not found. The problem is that the model hook stops the processing if the REST adapter returns an error.
I saw in the ember-data roadmap that error handling is not yet supported.
What I don't understand is why does Ember not call the redirect hook in the Route if an error occurs. (And how can I handle such errors?)
Here is the Route:
App.MyRoute = Ember.Route.extend({
renderTemplate: function() {
this.render('mymodel')
},
model: function(params) {
return App.MyModel.find(params.id);
},
redirect: function() {
// I want to redirect to another route
// if the model is not found via the find method.
// However, this hook is not called if the previous one
// does not return an object
}
})
Is there any other solution? I know, I can check in the template if the model of the view is null and then render a partial template, but the problem is that the view is not rendered at all (I assume exactly because of the same problem).
Upvotes: 1
Views: 1370
Reputation: 4969
What I would do is create an abstract, and then have your routes extend that route. I've created a working JSFiddle for you, but please let me explain.
Our App.IndexRoute
can contain all of the logic in App.MyRedirectRoute
, but because other routers might require the same functionality, it's better to create an abstract so if/when you need this functionality again, you can just extend App.MyRedirectRoute
again to prevent re-writing code.
Unfortunately Ember.JS doesn't have the logic to detect if the model is empty and to render a different page. Maybe in the future this will be a reality! (It would be lovely!), but we can do this ourselves.
Our App.IndexRoute
is nice and simple:
App.IndexRoute = App.MyRedirectRoute.extend({
defaultRender: 'home',
errorRedirectTo: '404',
model: function(params) {
// We've found a model!
return Ember.Object.create({ params: params });
// We've not found a model!
return null;
}
});
If the model is valid then we can render the home
route, otherwise we'll render the 404
route. Nothing changes in returning the model from the route.
It's the renderTemplate
that contains our logic for rendering the appropriate view. So we therefore overload the renderTemplate
method in our abstract and do the logic stuff:
errorRedirectTo
;defaultRender
.If the model is empty, then we'll simply specify that we wish to render the 404 route (errorRedirectTo
), but by default we'll want to render the default route (defaultRender
).
To see it in action, take a look at the aforementioned JSFiddle. Comment out line #27 to see the 404 page rendered, because the object is null
.
I hope this helps!
Upvotes: 1