Mar*_*arc 0 ember.js ember-data
我有一个在用户计算机上运行的 Ember 应用程序(带有 ember 数据)。他们的机器上运行着一个 API 服务器,还有一个在线运行。我需要允许用户选择模型的持久保存位置(在其计算机上运行的 API 或在线 API)。因此,有两个 API 主机:
http://localhost:3000
and
http://api.example.com
Run Code Online (Sandbox Code Playgroud)
当用户创建记录时,他们可以设置是将记录保存在本地(通过本地 API 服务器)还是在线保存。我将此选择保留为记录上名为 dataSource 的值。
因此,根据记录数据源,我需要将模型的 ember RestAdapter 主机设置为正确的值。我知道可以根据模型覆盖适配器。例如,我可以创建一个 RecordAdapter 并手动将主机设置为一个值。但是,主机取决于记录中的值,并且我不确定如何使用 Ember Data 来完成此操作,因为其余适配器“主机”是一个属性,而不是一个函数。
http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_host
用户流程示例:
您需要覆盖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;
},
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,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;
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1045 次 |
| 最近记录: |