基于https://guides.emberjs.com/v2.11.0/models/customizing-adapters/#toc_headers-customization,
import DS from 'ember-data';
import config from '../config/environment';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
session: Ember.inject.service(),
headers: Ember.computed({
get() {
let headers = {};
this.get('session')
.authorize('authorizer:custom', (headerName, headerValue) => {
headers[headerName] = headerValue;
});
return headers;
}
}),
queryRecord(modelName, query) {
return Ember.$.getJSON(`${config.host}/users/me`);
}
});
我在适配器中定义了一个headers
属性,然后请求发送到服务器,标头中没有Authentication
(因此我不断获取401
未经授权的错误)。
我尝试在queryRecord
方法中记录headers属性,但它确实显示了正确的标题信息。
答案 0 :(得分:1)
我认为问题是计算出的道具缓存。
你可以这样做:
headers: Ember.computed('session.isAuthenticated', {
get() {
let headers = {};
this.get('session')
.authorize('authorizer:custom', (headerName, headerValue) => {
headers[headerName] = headerValue;
});
return headers;
}
}),
或者
headers: Ember.computed({
get() {
let headers = {};
this.get('session')
.authorize('authorizer:custom', (headerName, headerValue) => {
headers[headerName] = headerValue;
});
return headers;
}
}).volatile(),