我有一个在用户机器上运行的Ember应用程序(带有余烬数据)。他们的机器上运行着一个API服务器,一个在线运行。我需要允许用户选择持久化模型的位置(在其计算机上运行的API或在线API)。所以,两个API主机:
http://localhost:3000
and
http://api.example.com
当用户创建记录时,他们可以设置他们希望本地保存的记录(通过本地API服务器),还是在线保存。我将此选项保留为名为dataSource的记录中的值。
因此,根据记录dataSource,我需要将模型的ember RestAdapter主机设置为正确的值。我知道可以在每个型号的基础上覆盖适配器。例如,我可以创建一个RecordAdapter并手动将主机设置为一个值。但是,主机依赖于记录中的值,并且我不确定如何使用Ember Data完成此操作,因为其余的适配器" host"是一种财产,而不是一种功能。
http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_host
用户流程示例:
答案 0 :(得分:4)
您需要覆盖适配器上的buildURL
:
根据Ember Data source,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配置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;
},
});