【问题标题】:default error template on Ember not loadingEmber 上的默认错误模板未加载
【发布时间】:2014-04-28 11:33:12
【问题描述】:

我不确定我是否做错了什么,但在我的 ApplicationRoute 我有类似的东西:

actions: {
    error: function(error, transition) {
        var self = this;
        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) {
                    throw new Error("Error");
                }
            }
        };

        if(error.statusText.toLowerCase() in metadata) {
            metadata[error.statusText.toLowerCase()].action(error, transition);
        }
    }
}

我有一个 error.hbs 模板,我希望在“错误网关”错误时触发它,但是模板没有加载,有什么具体的方法可以让我加载默认的错误模板吗?

谢谢。

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    如果您定义应用程序错误处理程序,则必须呈现错误模板。

    App.ApplicationRoute = Ember.Route.extend({
      actions: {
        error: function() {
    
          this.render('error', {
            into: 'application'
          });
        }
      }
    
    });
    

    一个例子是可用的here

    您可以删除错误处理程序并检查 ember 是如何呈现错误模板的。

    【讨论】:

    • 非常感谢,效果很好。在文档中找不到任何相关内容:/
    • 我现在遇到了一个问题,在将错误页面渲染到应用程序后,将您带到应用程序其他部分的其他链接都不起作用,出现以下错误:
    • 对于现在来到这里的任何人,我认为这应该是不必要的。您需要确保 action 处理程序返回 true 并且(如果您仍然使用 pod)您的 error.hbs 在您的 pods/templates/error.hbs 中而不是在 pods/application/error.hbs
    【解决方案2】:

    只需return true,因此错误不断冒泡:

    actions: {
      error(transition, originRoute) {
        return true;
      }
    }
    

    How to both have an action error and render template in EmberJS

    【讨论】:

    • 只是对此发表评论,我已经设置了这个但仍然无法让模板呈现,直到我注意到我的 error.hbs 在我的 pods/application/ 文件夹中而不是 pods/templates/文件夹
    【解决方案3】:

    添加到@ppcano 的答案:

    如果需要在错误模板上显示错误信息,将错误对象传入错误动作,如图here

    actions: {
      error: function(error) {
        // e.g. you can have a controller for the error route and set error.message and error.status here
        this.render('error', {
          into: 'application'
        });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      相关资源
      最近更新 更多