Controllers and routes
With logic running in a controller or route, you usually use the error action attached to the route. If the action is run on the controller and the controller does not process this action (for example, the action hash controller does not have an error action), it is automatically transferred to the route in order to be able to process it - if the current route does not process the action, or it bubbles on the parent routes, until it reaches ApplicationRoute .
As I process it, there is an AuthenticatedRoute from which any route that should handle 401 extends. In addition, in the example to which you are attached, it has events , whereas now actions .
Something like this should work for you:
App.AuthenticatedRoute = Ember.Route.extend({ actions: { error: function(error) { if (!error || error.status !== 401) { // returning true bubbles the error to parent routes return true; } // put your logic here console.log('this is a 401 error.'); } } }); App.IndexRoute = App.AuthenticatedRoute.extend({ //logic }); App.FooRoute = App.AuthenticatedRoute.extend({ //logic }); App.BarRoute = App.AuthenticatedRoute.extend({ //logic });
Components
Ember is moving more and more towards components - the thing with the logic executed in the components is that it is completely isolated from everything else. As a rule, the only thing that is available to you in your components is what you pass on to them.
This means that if you execute any logic in the component, then none of the above logic - the logic that exists on the routes for error handling, is used if you do not explicitly use it yourself.
If you call calls like store.find('person', 1) or person.save() in the component, you need to explicitly handle the errors using the .catch() function or pass the second function to .then() . For example, the following two statements will do the same:
store.find('person', 1).then(function(person) { console.log('found person 1:', person); }, function(err) { console.log('error finding person 1:', err); }); store.find('person', 1).then(function(person) { console.log('found person 1:', person); }).catch(function(err) { console.log('error finding person 1:', err); });
And so would these two statements:
person.save().then(function() { console.log('successfully saved person'); }, function(err) { console.log('error saving person:', err); }); person.save().then(function() { console.log('successfully saved person'); }).catch(function(err) { console.log('error saving person:', err); });
Reusing Route Error Logic in Components
If you want to transfer error handling in your components on your routes, in order to deal with the best way to do this, you need the component to start the action and process the calling template / view / controller.
app/components/my-component.js
Ember.Component.extend({ 'on-error': null, model: null, actions: { save: function() { var component = this; var model = this.get('model'); mode.save().then(function() { console.log('saved'); }).catch(function(err) { component.sendAction('on-error', err); }); } } });
app/templates/components/my-component.hbs
<button {{action 'save'}}>Save</button>
app/templates/index.hbs
{{my-component model=model on-error='error'}}