Ember-Data 处理 401 [英] Ember-Data handling 401’s

查看:18
本文介绍了Ember-Data 处理 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对处理 401 错误有什么想法吗?

Any thoughts on handling 401 errors?

在应用程序初始值设定项中,我推迟准备并通过 ember-data 获取当前用户.如果我收到 401,应用程序就会死机并无法使用.我想处理这个错误,然后提前准备.我似乎无法找到解决方法.任何信息将不胜感激!

In the application initializer I'm deferring readiness and fetching the current user via ember-data. If I receive a 401 the app dies and becomes unusable. I'd like to handle this error, and then advancereadiness. I cant seem to find a workaround for this. Any info would be appreciated!

要点在这里:https://gist.github.com/unknpwn/6126462

我注意到这里有一个类似的话题,但似乎已经结束日期.

I noticed there was a similar topic here, but it seems to be out of date.

推荐答案

Application.initializer 不是放置此逻辑的正确位置.它是同步的,其目的是将自定义对象添加到 IOC 容器等.此代码更适合在 ApplicationRoutemodel 挂钩中.

Application.initializer is not the correct place to put this logic. It is synchronous and it's purpose is to do things like adding custom objects to the IOC container, etc. This code is better suited in the model hooks of ApplicationRoute.

App.ApplicationRoute = Ember.Route.extend({
  beforeModel: function() {
    return App.User.find({ filter: "authenticated" });
  },
  setupController: function(controller, model) {
    controller.set('loggedIn', true);
    controller.set('currentUser', model);// or some property of that model
  },
  events: function() {
    error: function(err) {
      // error handler called in case of an error.
      // show the error message to user here
      // or transition to another route
    }
  }
});

这篇关于Ember-Data 处理 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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