在RESTAdapter初始化后添加头 [英] Adding headers after RESTAdapter initialization

查看:116
本文介绍了在RESTAdapter初始化后添加头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在适配器初始化和使用之后,我试图向我的适配器请求添加一个授权头。我可以在创建我的 ApplicationAdapter 时以静态方式添加头文件,但是我似乎无法在后续的REST调用中使用头文件。我正在尝试:

  var auth =Basic+ hash; 
App.ApplicationAdapter.reopen({
headers:{
授权:auth
}
});

我已经调用了 RESTAdapter c $ c> ajax 方法,而 adapter.headers 的测试始终为 undefined

解决方案

接受的答案并不涉及推荐的方法在ember-data中不起作用的事实。我以后推荐:



https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L88



https:// github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L162
和该文件中的其他地方。



此外,OP提出的问题具体出现在这里:
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L619



所以,以下内容根本不起作用:

  App.ApplicationAdapter .reopen({
headers:{token:'reopen_token(NO WORK)'}
});

我已经尝试指出这是一个问题,但在一小时内关闭:
https://github.com/emberjs/data/issues/1820



希望核心将决定修正或删除评论。但是,是的,现在看来,你必须劫持jQuery ajax设置,Ember。$ ajaxPrefilter,或者自己覆盖适配器上的 ajax

$ b因此,在从Ember开发者获得更多反馈后,看起来这个问题的核心是尝试重新打开已创建的实例。所以在定义时使用一个计算的属性(因此它会根据需要进行更新)似乎是建议的方法。希望有所帮助(最近有一个合并的拉动请求,这在引用文件的注释中更为明显: https://github.com/emberjs/data/pull/1818/files#diff-1d7f5a5b77898df15de501c3c38d4829R108



编辑2:在我的应用程序中有这个工作,所以这里的代码,以防其他人被卡住:

  // app.js 
App.ApplicationAdapter = DS.ActiveModelAdapter.extend({
namespace:'api / v1',
headers:function(){
return {
token:this.get( 'App.authToken')|| localStorage.getItem('token')
};
} .property(App.authToken)
});

//login-controller.js(only action shown..assume`data` has user / pass)
actions:{
login:function(){
$ .post('/ token /',data).done(function(user){
App.set('authToken',user.token);
//以上将触发适配器的头部计算属性更新

//转换到以前尝试的路由
var attemptsTransition = self.get('attemptsTransition');
if(attemptsTransition){
attemptTransition.retry ();
}
else {
self.transitionToRoute('yourapproute');
}
})
.fail(function(response){
//失败处理省略
});


I am trying to add an Authorization header to my adapter's request after the adapter has been initialized and used. I can add headers in a static way at the time I create my ApplicationAdapter, but I can't seem to get it use the headers in subsequent REST calls. I am trying this:

var auth= "Basic " + hash;
App.ApplicationAdapter.reopen({
    headers: {
        Authorization: auth
    }
});

I have debugged RESTAdapter in the ajax method, and the test for adapter.headers is always undefined.

解决方案

The accepted answer doesn't address the fact that the recommended approach is not working in ember-data. I say recommended since:

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L88

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L162 and other places in that file.

Further, the issue the OP brings up with of undefined specifically happens here: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L619

So, the following simply does not work:

App.ApplicationAdapter.reopen({
  headers: {token: 'reopen_token (NO WORK)' }
});

I've tried to point to this out as an issue but it got closed within an hour: https://github.com/emberjs/data/issues/1820

Hopefully core will decide to either fix this or remove the comments. But, yes, for now it seems you have to hijack jQuery ajax setup, Ember.$.ajaxPrefilter, or override the ajax on the adapter yourself.

EDIT: So after getting some more feedback from Ember devs, it looks like the core of this issue is trying to reopen an instance already created. So using a computered property when it's defined (so it will update as desired) seems to be the advised approach. Hope that helps (there's a recently merged pull request that makes this more obvious in the comments of referenced file:https://github.com/emberjs/data/pull/1818/files#diff-1d7f5a5b77898df15de501c3c38d4829R108 )

EDIT 2: Got this working in my app so here's the code in case someone else gets stuck:

//app.js
App.ApplicationAdapter = DS.ActiveModelAdapter.extend({
  namespace: 'api/v1',
  headers: function() {
    return {
      token: this.get('App.authToken') || localStorage.getItem('token')
    };
  }.property("App.authToken")
});

//login-controller.js (only action shown..assume `data` has user/pass)
  actions: {
    login: function() {
        $.post('/token/', data).done(function(user) {
          App.set('authToken', user.token);
          //Above will trigger adapters's header computed property to update

          // Transition to previous attempted route
          var attemptedTransition = self.get('attemptedTransition');
          if(attemptedTransition) {
            attemptedTransition.retry();
          }
          else {
            self.transitionToRoute('yourapproute');
          }
        })
        .fail(function(response) { 
          //fail handling omitted
        });

这篇关于在RESTAdapter初始化后添加头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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