Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 如何使用余烬数据支持多个API主机?_Ember.js_Ember Data - Fatal编程技术网

Ember.js 如何使用余烬数据支持多个API主机?

Ember.js 如何使用余烬数据支持多个API主机?,ember.js,ember-data,Ember.js,Ember Data,我有一个在用户机器上运行的余烬应用程序(带有余烬数据)。有一个API服务器在他们的机器上运行,还有一个在线运行。我需要允许用户选择模型的持久化位置(在他们的机器上运行的API或在线API)。因此,有两个API主机: http://localhost:3000 and http://api.example.com 当用户创建记录时,他们可以设置是希望记录保存在本地(通过本地API服务器),还是在线保存。我将此选择持久化为记录上名为dataSource的值 因此,根据记录数据源,我需要将模型的em

我有一个在用户机器上运行的余烬应用程序(带有余烬数据)。有一个API服务器在他们的机器上运行,还有一个在线运行。我需要允许用户选择模型的持久化位置(在他们的机器上运行的API或在线API)。因此,有两个API主机:

http://localhost:3000
and
http://api.example.com
当用户创建记录时,他们可以设置是希望记录保存在本地(通过本地API服务器),还是在线保存。我将此选择持久化为记录上名为dataSource的值

因此,根据记录数据源,我需要将模型的ember RestaAdapter主机设置为正确的值。我知道每个型号都可以覆盖适配器。例如,我可以创建一个RecordAdapter并手动将主机设置为一个值。但是,主机依赖于记录中的一个值,我不确定如何使用余烬数据实现这一点,因为rest适配器“主机”是一个属性,而不是一个函数

用户流示例:

  • 用户创建新记录,选择本地存储此记录的选项
  • 记录被持久化到本地主机
  • 用户创建新记录,选择联机存储此记录的选项
  • 记录被持久化到
  • 无论何时保存、更新、删除记录等,都必须检查记录数据源以确定操作中要使用的api主机

您需要覆盖适配器上的
buildURL

根据Ember数据源,RestaAdapter的默认实现为:

/**
    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;
  },
});

您需要覆盖适配器上的
buildURL

根据Ember数据源,RestaAdapter的默认实现为:

/**
    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;
  },
});