最后,除了覆盖之外,我找不到其他解决方案RESTAdapter.ajax
。我最终添加了三个参数auth[token]
:auth[school]
和auth[name]
。
DS.RESTAdapter.reopen({
/* Override to add the authToken, school and name */
ajax: function(url, type, hash) {
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
hash = hash || {};
hash.url = url;
hash.type = type;
hash.dataType = 'json';
hash.context = adapter;
if (hash.data && type !== 'GET') {
hash.contentType = 'application/json; charset=utf-8';
/* Add the data to the hash before it's stringified. */
if (HstryEd.Session.get('isLoggedIn')) {
hash.data.auth = {};
hash.data.auth.token = HstryEd.Session.get('authToken');
hash.data.auth.school = HstryEd.Session.get('currentUser').get('school');
hash.data.auth.name = HstryEd.Session.get('currentUser').get('name');
}
hash.data = JSON.stringify(hash.data);
}
if (adapter.headers !== undefined) {
var headers = adapter.headers;
hash.beforeSend = function (xhr) {
forEach.call(Ember.keys(headers), function(key) {
xhr.setRequestHeader(key, headers[key]);
});
};
}
hash.success = function(json) {
Ember.run(null, resolve, json);
};
hash.error = function(jqXHR, textStatus, errorThrown) {
if (jqXHR) {
jqXHR.then = null;
}
Ember.run(null, reject, jqXHR);
};
Ember.$.ajax(hash);
});
}
});