如何保持应用程序工作,即使模型未能加载烬

How to keep app working even if model fails to load on Ember

本文关键字:模型 加载 何保持 应用程序 工作      更新时间:2023-09-26

所以,在我的ApplicationRoute上,我正在加载一个模型来加载整个应用程序常见的一些数据。工作流程如下:

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('config');
    },
    actions: {
        error: function(error, transition) {
            var self = this,
                renderErrorPage = function(template, disableMenu) {
                    self.render(template, {
                        into: 'application'
                    });
                    if(disableMenu) {
                        $('.menu > a').addClass('disable-menu'); // disable menu links
                    }
                },
            metadata = {
                'timeout': {
                    action: function(error, transition) {
                       BootstrapDialog.alert({message: 'Request Timeout!'});
                    }
                },
                'forbidden': {
                    action: function(error, transition) {
                        self.transitionTo('companies');
                    }
                },
                'unauthorized': {
                    action: function(error, transition) {
                        window.location.replace(error.responseJSON.redirect_url);
                    }
                },
                'bad gateway': {
                    action: function(error, transition) {
                        renderErrorPage('error', true);
                    }
                },
                'internal server error': {
                    action: function(error, transition) {
                        renderErrorPage('error', true);
                    }
                },
                'not found': {
                    action: function(error, transition) {
                        renderErrorPage('404', false);
                    }
                }
            };
            if(error.statusText.toLowerCase() in metadata) {
                metadata[error.statusText.toLowerCase()].action(error, transition);
            }
        }
    }
});

所以,如果用户没有登录,他得到一个unauthorized 401错误,重定向到登录页面,如果他登录了,但仍然没有访问这个应用程序,他得到一个forbidden 403错误,只是做一个过渡到/#/companies/,他将注册他的公司,然后有权限访问完整的应用程序。

我的问题是:这个过渡到companies没有发生,因为模型无法加载,我不知道如何修复它。

在我看来,你可能应该找到一种不同的方式来加载配置,而不是通过请求一个经过身份验证的API(或者至少为不需要身份验证的路由提供一个开放版本,比如这个错误页面)。你可以只使用

if (isAuthenticated) {
    return this.store.find('config');
}

…然后将isAuthenticated设置在auth进程的某个位置