Ember: 如何 rewrite/extend RESTAdapter 使其在站点范围内可用?

Ember: How to rewrite/extend RESTAdapter to make it site wide?

如果我在 app/adapters/application.js

中扩展适配器
let appAdapter = DS.RESTAdapter.extend({
  ajax: function(url, method, hash) {
    hash = hash || {};
    hash.crossDomain = true;
    hash.xhrFields = {
        withCredentials: true
    };
    return this._super(url, method, hash);
  },
});

export default appAdapter.reopen(config.adapterSettings);

它仍将被我也有的特定型号适配器所取代。

我有几个特定的​​适配器,例如:app/adapters/testpost.js

export default DS.RESTAdapter.extend(myTemplates, {
  myTemplate: `${host}/${dir}/testpost`,
});

现在为了让它工作,我用相同的代码扩展了它们中的每一个,例如 app/adapters/testpost.js 变成了:

let testpostAdapter = DS.RESTAdapter.extend({
  ajax: function(url, method, hash) {
    hash = hash || {};
    hash.crossDomain = true;
    hash.xhrFields = { 
        withCredentials: true
    };
    return this._super(url, method, hash);
  },
});

export default testpostAdapter.extend(myTemplates, {
  myTemplate: `${host}/${dir}/testpost`,
});

问题: 如何 extend/rewrite RESTAdapter 用于所有 Ember,以及一次用于所有特定适配器。

尝试在 app/app.js 中进行扩展,那样行不通。

不应该直接从模型特定适配器中的DS.RESTAdapter.派生,而是从您的应用程序适配器派生。所以替换这个:

import DS from 'ember-data';
export default DS.RESTAdapter.extend({...})

在您的特定型号适配器中:

import ApplicationAdapter from './application'; // assuming not in pod structure
export default ApplicationAdapter.extend({...});