您需要覆盖适配器上的buildURL:
根据 Ember 数据源,RESTAdapter 的默认实现是:
/**
Builds a URL for a given type and optional ID.
By default, it pluralizes the type's name (for example, 'post'
becomes 'posts' and 'person' becomes 'people'). To override the
pluralization see [pathForType](#method_pathForType).
If an ID is specified, it adds the ID to the path generated
for the type, separated by a `/`.
@method buildURL
@param {String} type
@param {String} id
@param {DS.Model} record
@return {String} url
*/
buildURL: function(type, id, record) {
var url = [],
host = get(this, 'host'),
prefix = this.urlPrefix();
if (type) { url.push(this.pathForType(type)); }
//We might get passed in an array of ids from findMany
//in which case we don't want to modify the url, as the
//ids will be passed in through a query param
if (id && !Ember.isArray(id)) { url.push(encodeURIComponent(id)); }
if (prefix) { url.unshift(prefix); }
url = url.join('/');
if (!host && url) { url = '/' + url; }
return url;
},
如您所见,适配器上的buildURL 提供了记录,因此可以根据该记录配置 URL。
以下示例显示了如何根据模型实例上的 isLocal 属性选择前缀:
// app/adapters/application.js
import Ember from 'ember';
import DS from 'ember-data';
var get = Ember.get;
export default DS.RESTAdapter.extend({
buildURL: function(type, id, record) {
var url = [],
host = get(this, 'host'),
prefix;
// choose prefix based on model setting
if (record && get(record, 'isLocal')) {
prefix = 'http://localhost:3000';
} else {
prefix = this.urlPrefix();
}
if (type) { url.push(this.pathForType(type)); }
//We might get passed in an array of ids from findMany
//in which case we don't want to modify the url, as the
//ids will be passed in through a query param
if (id && !Ember.isArray(id)) { url.push(encodeURIComponent(id)); }
if (prefix) { url.unshift(prefix); }
url = url.join('/');
if (!host && url) { url = '/' + url; }
return url;
},
});