Ember Data复数序列化器

时间:2016-07-11 11:32:11

标签: ember.js

我有一个product模型和一个/products/路由 - 但是ember数据会向host/api/v1/product而不是host/api/v1/products发送请求 - 这是为什么?

另外,如何使用复数终点进行单个产品获取?即:host/api/v1/product/1

2 个答案:

答案 0 :(得分:1)

检查 buildURL 方法

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).

并且有子方法列表(https://github.com/emberjs/data/blob/v2.6.1/addon/-private/adapters/build-url-mixin.js#L54

switch (requestType) {
  case 'findRecord':
    return this.urlForFindRecord(id, modelName, snapshot);
  case 'findAll':
    return this.urlForFindAll(modelName, snapshot);
  case 'query':
    return this.urlForQuery(query, modelName);
  case 'queryRecord':
    return this.urlForQueryRecord(query, modelName);
  case 'findMany':
    return this.urlForFindMany(id, modelName, snapshot);
  case 'findHasMany':
    return this.urlForFindHasMany(id, modelName, snapshot);
  case 'findBelongsTo':
    return this.urlForFindBelongsTo(id, modelName, snapshot);
  case 'createRecord':
    return this.urlForCreateRecord(modelName, snapshot);
  case 'updateRecord':
    return this.urlForUpdateRecord(id, modelName, snapshot);
  case 'deleteRecord':
    return this.urlForDeleteRecord(id, modelName, snapshot);
  default:
    return this._buildURL(modelName, id);
}

所以你可以为你的目的重新定义任何

答案 1 :(得分:0)

更新您的适配器如下:

import JSONAPIAdapter from 'ember-data/adapters/json-api';

export default JSONAPIAdapter.extend({
    pathForType: function(type) {
       var camelized = Ember.String.camelize(type);
       return Ember.String.singularize(camelized);
     }
});