Ember模型主机适配器不会覆盖海市蜃楼

时间:2016-10-19 23:01:28

标签: ember.js ember-cli-mirage

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

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

我已使用以下内容设置adapter / 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?

我的标题检查员显示客户正在向海市蜃楼服务器发出请求:

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的变体作为潜在的解决方法。但我不明白为什么海市蜃楼不会接受适配器。

1 个答案:

答案 0 :(得分:0)

应该通过海市蜃楼文件回读这个。有一个passthrough function允许海市蜃楼通过某些请求通过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'
});

如果这有助于您以任何方式随意给这个答案一个upvote:)