在 RESTAdapter 初始化后添加标头 [英] Adding headers after RESTAdapter initialization
问题描述
我正在尝试在适配器初始化和使用后将 Authorization
标头添加到我的适配器请求中.我可以在创建 ApplicationAdapter
时以静态方式添加标头,但我似乎无法让它在后续 REST 调用中使用标头.我正在尝试这个:
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
}
});
我在ajax
方法中调试了RESTAdapter
,对adapter.headers
的测试总是undefined
.
I have debugged RESTAdapter
in the ajax
method, and the test for adapter.headers
is always undefined
.
推荐答案
接受的答案没有解决推荐方法在 ember-data 中不起作用的事实.我说推荐因为:
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以及该文件中的其他位置.
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L162 and other places in that file.
此外,OP 提出的未定义问题具体发生在这里:https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L619
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)' }
});
我试图指出这是一个问题,但它在一个小时内关闭了:https://github.com/emberjs/data/issues/1820
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
希望核心将决定修复此问题或删除评论.但是,是的,现在看来您必须劫持 jQuery ajax 设置 Ember.$.ajaxPrefilter,或者自己覆盖适配器上的 ajax
.
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.
所以在从 Ember 开发者那里得到更多反馈之后,看起来这个问题的核心是试图重新打开一个已经创建的实例.因此,在定义时使用计算机属性(因此它将根据需要进行更新)似乎是建议的方法.希望有所帮助(最近合并的拉取请求在引用文件的注释中更加明显:https://github.com/emberjs/data/pull/1818/files#diff-1d7f5a5b77898df15de501c3c38d4829R108 )
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 )
编辑 2: 在我的应用程序中使用此功能,因此这里是代码,以防其他人卡住:
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屋!