0

我正在使用 Ember-cli-mirage 来模拟数据。我想慢慢整合位于我本地机器上http://localhost:8000的生产 api 的一部分。Ember 文档告诉我,我应该能够设置一个适配器,这样我就可以为每个模型使用不同的主机。

我有一个customer模型,并设置了成功提供数据的 ember-cli-mirage。客户模型是我要拆分到 localhost:8000 的第一个模型。

我已经使用以下内容设置了 adapters/customer.js:

import DS from 'ember-data';

export default DS.RESTAdapter.extend( {
  host: 'http://localhost:8000',
  namespace: 'api/v1'
});

但是当我拨打电话时,我收到了一个错误:

Mirage: Error: Your Ember app tried to GET 'http://localhost:8000/api/v1/customers',
         but there was no route defined to handle this request.
         Define a route that matches this path in your
         mirage/config.js file. Did you forget to add your namespace?

我的标头检查器显示客户正在向 mirage 服务器发出请求:

Request URL:http://localhost:6543/customers
Request Method:GET
Status Code:304 Not Modified
Remote Address:[::1]:6543

我怀疑这与我的 config/environment.js 设置有关,所以我正在查看https://github.com/samselikoff/ember-cli-mirage/issues/497#issuecomment-183458721的变体作为潜在的解决方法. 但我不明白为什么海市蜃楼不接受适配器覆盖。

4

1 回答 1

0

应该已经阅读了这个海市蜃楼的文档。有一个直通功能允许 mirage 绕过 mirage 将某些请求传递给 Ember:

// mirage/config.js
import Mirage from 'ember-cli-mirage';

export default function() {

  this.urlPrefix = 'http://localhost:8000';
  this.namespace = '/api/v1';

  // Requests for customers
  this.get('/customers');
  this.get('/customers/:id');
  this.post('/customers');
  this.del('/customers/:id');
  this.patch('/customers/:id');

  // Passthrough to Django API
  this.passthrough('/customers');

}

为了在我的应用程序适配器中进行这项工作,我添加了:

// app/adapters/application.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  host: 'http://localhost:8000',
  namespace: 'api/v1'
});

如果这对您有任何帮助,请随时给这个答案投票:)

于 2016-10-20T00:12:23.087 回答