【问题标题】:Transitioning when encountering an error in Ember在 Ember 中遇到错误时转换
【发布时间】:2014-01-06 12:38:19
【问题描述】:

我正在尝试在ApplicationRoute 错误操作中执行this.transitionTo('route name')

似乎发生的情况是,当我单击链接时,它遇到了错误,并且没有转换到目标路线,似乎说它正在转换到它刚刚所在的路线。这里使用的是{{#link-to}},所以我感觉里面有一些魔法……

我正在尝试使用this example

错误发生在路由上的 model() 方法中(它使用 jquery 并返回一个承诺 [jqXHR]),因为我返回的是 401 http 代码。

这不正确吗?

App.ApplicationRoute = Ember.Route.extend(
    actions:
        error: (error, transition) ->
            if error.status == 401
                @transitionTo('login')
)

我尝试过的另一件事是设置.ajaxError 方法并在其中转换,但结果似乎也没有转换。

App.ApplicationRoute = Ember.Route.extend(
    setupController: (controller, model) ->
        route = @
        Ember.$(document).ajaxError((event, jqXHR, ajaxSettings, error) ->
            if jqXHR.status == 401
                route.transitionTo('login')
        )

        controller.set('content', model)
)

有人在 ember 1.2.0 上完成这项工作吗?

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    尝试在错误挂钩中返回true,以冒泡错误事件。

    来自文档 (http://emberjs.com/guides/routing/asynchronous-routing/#toc_when-promises-reject):

    actions: {
      error: function(reason) {
        alert(reason); // "FAIL"
    
        // Can transition to another route here, e.g.
        // this.transitionTo('index');
    
        // Uncomment the line below to bubble this error event:
        // return true;
      }
    }
    

    【讨论】:

      【解决方案2】:

      原来这是我的问题。我有一些额外的逻辑基本上是在反击我的过渡!哎呀!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2021-11-02
        • 1970-01-01
        相关资源
        最近更新 更多