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

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

问题描述

处理401错误的任何想法?



在应用程序初始化程序中,我推迟准备并通过ember-data获取当前用户。如果我收到一个401,该应用程序死亡,变得不可用。我想处理这个错误,然后高级的。我似乎找不到解决办法。任何信息将不胜感激!



Gist here: https://gist.github.com/unknpwn/6126462



我注意到有一个

Application.initializer 不是放置此逻辑的正确位置。它是同步的,它的目的是做诸如向IOC容器等添加自定义对象之类的东西。该代码更适合模型钩子 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); //或该模型的某些属性
},
事件:function(){
error:function(err){
//发生错误时调用错误处理程序
//向用户显示错误消息
//或转换到另一个路由
}
}
});


Any thoughts on handling 401 errors?

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!

Gist here: https://gist.github.com/unknpwn/6126462

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

解决方案

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天全站免登陆