【问题标题】:How to support multiple API hosts with Ember data?如何使用 Ember 数据支持多个 API 主机?
【发布时间】:2014-11-11 03:37:47
【问题描述】:

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

http://localhost:3000
and
http://api.example.com

当用户创建记录时,他们可以设置是希望将记录保存在本地(通过本地 API 服务器)还是在线保存。我将此选择保留在名为 dataSource 的记录上的一个值上。

因此,根据记录数据源,我需要将模型的 ember RestAdapter 主机设置为正确的值。我知道可以在每个模型的基础上覆盖适配器。例如,我可以创建一个 RecordAdapter 并手动将主机设置为一个值。但是,主机取决于记录中的一个值,我不确定如何使用 Ember Data 来完成此操作,因为其余适配器“主机”是一个属性,而不是一个函数。

http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_host

用户流示例:

  • 用户创建新记录,选择在本地存储此记录的选项。
  • 记录保存到本地主机。
  • 用户创建新记录,选择在线存储此记录的选项。
  • 记录保存到http://api.example.com
  • 任何时候保存、更新、删除记录等,都必须检查记录数据源以确定操作中要使用的 api 主机。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    您需要覆盖适配器上的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;
      },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2016-09-17
      • 1970-01-01
      相关资源
      最近更新 更多