Ember 节省需求 headers。似乎没有通过 RESTAdapter headers 函数

Ember save needs headers. Doesnt seem to pass thru RESTAdapter headers function

我需要通过 header 传递一个授权令牌,这对 findfindQuery 工作正常,但 save 似乎没有调用 header 功能?!

createRecord: function(store, type, record) {
   var data = {};
   var serializer = store.serializerFor(type.typeKey);

   var snapshot = record._createSnapshot();
   serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

   return this.ajax(this.buildURL(type.typeKey, null, record), "POST", { data: data });
},

在上面的 RESTAdapter

中没有调用 header

我可以很容易地添加一个 header 调用,但是......确实很奇怪它丢失了。

而不是覆盖存储方法扩展RESTAdapter

App.ApplicationAdapter = DS.RESTAdapter.extend({
  headers: {
    'API_KEY': 'secret key',
    'ANOTHER_HEADER': 'Some header value'
  }
});

Arbitrary headers can be set as key/value pairs on the RESTAdapter's headers property and Ember Data will send them along with each ajax request.

http://emberjs.com/guides/models/connecting-to-an-http-server/#toc_custom-http-headers

是的,重写 createRecord 很简单。在 RESTAdapter 中缺少它似乎有点奇怪。

createRecord: function(store, type, record) {
   var data = {};
   var serializer = store.serializerFor(type.typeKey);

   var snapshot = record._createSnapshot();
   serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

   var headers = this.headers(); // Add this

   return this.ajax(this.buildURL(type.typeKey, null, record), "POST", { headers: headers, data: data }); 
 // And note the headers: data versus data only
},