如何在EmberJS中都有动作错误和渲染模板 [英] How to both have an action error and render template in EmberJS

查看:76
本文介绍了如何在EmberJS中都有动作错误和渲染模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的EmberJS应用程序中,当我无法访问资源时,我使用各种错误模板来呈现特定的错误消息。例如,当我尝试访问无效的重置密码时,我有一个名为reset-password / error.hbs的模板。



这个工作真的很好。



但是今天我在ApplicationRoute中添加了一个错误操作。目标是抓住一个可能在所有页面(401错误)中发生的非常具体的错误,并且我想使用它来使我的会话无效。因此,在我的应用程序路由中,我有以下代码:

 导出默认值Ember.Route.extend({
操作:{
error:function(result){
if(result.status === 401){
this.get('session')。invalidate();
}
}
}
});

问题是,使用此代码,我的错误模板reset-password不再显示。天真地,我认为Ember在reset-password / index路由中发生错误,将会是:


  1. 首先检查一个错误操作ResetPasswordIndexRoute。

  2. 然后尝试渲染reset-password / error.hbs(如果存在)。

  3. 然后起泡...直到达到应用程序,最后在ApplicationRoute中找到错误操作。

但似乎尽快定义错误动作,即使它在层次结构中高得多,它完全替代了可能在这里的任何模板。



我如何解决这个问题,还有两个错误处理程序?我试图在错误动作中调用_super,但没有结果。



谢谢!

解决方案

如果有人陷入这个问题,Twitter上的某人给了我答案:你在错误中添加了一个return true为了泡泡默认行为!

 导出默认值Ember.Route.extend({
actions:{
error:function(result){
if(result.status === 401){
this.get('session')。invalidate();
}
return true;
}
}
});


In my EmberJS application, I am using the various "error" template to render specific error messages whenever I cannot access a ressource. For instance, I have a template called "reset-password/error.hbs" that is rendered when I try to access an invalid reset password.

This works really well.

However today, I added an "error" action in my ApplicationRoute. The goal was to catch a very specific error that could potentially happening in all pages (the 401 error), and I want to use this to invalidate my session. Therefore, in my application route, I have the following code:

export default Ember.Route.extend({
   actions: {
      error: function(result) {
         if (result.status === 401) {
            this.get('session').invalidate();
         }
      }
   }
});

The problem is that with this code, my error template for "reset-password" is no longer shown. Naively, I thought that Ember, given an error occurs on "reset-password/index" route, would:

  1. First check on a "error" action on "ResetPasswordIndexRoute".
  2. Then try to render "reset-password/error.hbs" if it exists.
  3. Then bubbles up... until it reaches "application", and finally find the "error" action in ApplicationRoute.

But it seems that as soon an "error" action is defined, even if it's much higher in the hierarchy, it completely replaces any template that could be here...

How could I work around this, and still have the two error handler? I've tried to called "_super" in the error action, but no result.

Thanks!

解决方案

If anyone stumble into this problem, someone on Twitter gave me the answer: you have add a "return true" in the error action in order to bubble the default behaviour!

export default Ember.Route.extend({
   actions: {
      error: function(result) {
         if (result.status === 401) {
            this.get('session').invalidate();
         }
         return true;
      }
   }
});

这篇关于如何在EmberJS中都有动作错误和渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆