アダプターでの移行の代わりに、私見の良い実践ではありません。インスタンスを返し、現在のルートError
のアクションでそれを処理できます。error
App.UnauthorizedError // create a custom Error class
App.ApplicationAdapter = DS.RESTAdapter.extend({
ajaxError: function(jqXHR) {
var defaultAjaxError = this._super(jqXHR);
if (jqXHR) {
switch(jqXHR.status) {
case 401:
return new App.UnauthorizedError()
}
}
return defaultAjaxError;
}
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('person');
},
actions: {
error: function(reason) {
// all errors will be propagated to here, we check the instance to handle the UnauthorizedError
if (reason instanceof App.UnauthorizedError) {
this.transitionTo('login')
}
}
}
});
これをすべてのルートで使用したい場合は、無許可トランジションをApplicationRoute
. ApplicationRoute はすべてのルートの親であるため、処理されないアクション、または true を返すアクションは、親ルートにバブリングします。
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function(reason) {
if (reason instanceof App.UnauthorizedError) {
this.transitionTo('login')
}
}
}
});
App.BarRoute = Ember.Route.extend({
actions: {
error: function(reason) {
// handle errors of bar route
// bubble to application route
return true;
}
}
});
これは、このサンプルのフィドルですhttp://jsfiddle.net/SkCH5/