Reputation: 661
(This same text is open in EmberJS Discuss area too)
I have the following Route:
App.IndexCrmPersonEditRoute = Ember.Route.extend({
model: function(params) {
var person = this.store.find("person", params.id);
return person;
},
});
which receives the following Router path
...
this.route('edit', { path: ':id'}); // edit person
...
That works nicely for existing people. When the object doesn't exist, the API returns a 404 with the following content:
{"person": null}
an error "Error while loading route: undefined " happens in JS.
I wish to know if returning 404 + person = null is the correct approach and if it is possible to show a message and redirect the user back to the previous URL (or even not move them if the response is 404).
Any idea?
Upvotes: 3
Views: 2385
Reputation: 661
Well, thanks to the 2 answers above, I found out the solution with the Router's "error" action, which I describe below.
The documentation is not very good but here is what I made to implement:
I wrote a generic Route class to be extended with the "error" action:
App.NotFoundObjectRoute = Ember.Route.extend({
actions: {
error: function(reason, transition) {
if (reason.status == 404)
return true
else
this.transitionTo('r500');
},
},
});
Each route I expect to have the "Not found" beheaviour, I wrote and Error route, as below (attention to the word "Error" that automatically replaces "Edit", as EmberJS' standard:
App.IndexCrmPersonEditRoute = App.NotFoundObjectRoute.extend({
// ...
});
App.IndexCrmPersonErrorRoute = Ember.Route.extend({
// Called for errors like 404 (not found) and 500 (server error)
});
That turned to write also an error view, as below:
App.IndexCrmBusinessErrorView = Em.View.extend({
templateName: "r404",
});
I used the standard "r404" template for that, but as you can see it could be a different one for each different object type.
The "r500" route acts in case of server error (other than 404).
I hope that can be helpful for others.
Upvotes: 5
Reputation: 2861
1) If the promise in your model hook fails, the transaction will not be completed. So, you do not need to transaction back.
2) You could manage the error in your route error handler.
3) Your response does not need to be {person: null}, providing a status code 404 would be enough. In that case, your ajax promise will fail because you return a http error code.
http://emberjs.jsbin.com/kimol/1/edit
Upvotes: 2