[英]Accessing the error model in Ember ErrorRoute
我正在嘗試為Ember應用程序中的所有失敗路由編寫處理程序。
這里的文檔似乎建議我可以在我的App對象上創建ErrorRoute,當在另一條路由上的路由失敗時,它將自動轉換為。 如果路由失敗的原因是由於身份驗證問題(例如令牌超時),我想用它來將用戶重定向到登錄頁面。
我的問題是在ErrorRoute內部,我似乎沒有任何方法來訪問由失敗的路由返回的錯誤。 在將他們重定向到登錄屏幕之前,我想確定是否是身份驗證錯誤,我不確定該怎么做。
這是我為測試編寫的內容:
App.ErrorRoute = Ember.Route.extend({
activate: function() {
console.log("MODEL A", this.modelFor('error'));
setInterval(function() {
console.log("MODEL B", this.modelFor('error'));
}.bind(this), 1000);
}
});
激活路由后,控制台將嘗試訪問App.ErrorController的模型(由於某些原因尚未設置),因此會記錄MODEL A undefined
的MODELA。 一秒鍾后,將觸發MODEL B
控制台日志,並且已設置錯誤模型。
如果我無法訪問Route的activate
方法中的錯誤,那么我在哪里可以訪問它? 大概我不應該在超時中包裝邏輯。
您可以在當前活動層次結構的任何路由中管理錯誤。
通常,您在應用程序路由上設置錯誤處理程序以執行應用程序錯誤邏輯。
App.AccountRoute = Ember.Route.extend({
afterModel: function() {
return new Em.RSVP.Promise(function(resolve, reject){
throw new AuthenticatedError('error message');
});
}
});
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function(error) {
if ( error instanceof AuthenticatedError ) {
this.transitionTo('login');
} else {
// if return true, the event will bubble and transition to error
return true;
}
}
}
});
錯誤模型將傳遞到錯誤路由的renderTemplate(controller, model)
方法。
如果您不在乎登陸*.error
路由子狀態,則無論如何都要進行重定向,@ ppcano的答案就足夠了。 但是有時您希望將所有錯誤處理封裝在專用的錯誤路由對象中。 然后renderTemplate
是您處理事情的地方。
我同意,如果它的鈎子具有正確的modelFor(...)
會很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.