1

所以我正在构建我们网站的前端,长话短说,我们的 API 调用的位置是通过apigility。我对 Ember 还很陌生,所以我不得不将很多信息拼凑在一起,以找到完成这项工作的最佳方式。apigility 设置为提供 REST 服务,但输出格式如下:

{
  _links: {
    self: {
      href: "http://localhost:8888/article?page=1"
    },
    first: {
       href: "http://localhost:8888/article"
    },
    last: {
      href: "http://localhost:8888/article?page=1"
    }
  },
  _embedded: {
    article: [
      {
        nid: 1,
        body_value: "Lorem Ipsum etc etc",
        created: 1487176722,
        _links: {
          self: {
            href: "http://localhost:8888/article/1"
          }
        }
      }
    ]
  },
  page_count: 1,
  page_size: 25,
  total_items: 1,
  page: 1
}

首先,因为我只是在尝试进行概念验证,所以这是我的序列化程序:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
    primaryKey: 'nid',
    normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {   
        return {
            data: payload._embedded.article,                
        };
    }
});

我还在为我的适配器使用 RESTAdapter。

最后是我的 article.js 模型

import DS from 'ember-data';

export default DS.Model.extend({
    body_value: DS.attr('string'),
    nid: DS.attr('number')
});

它目前给我的错误是:

You must include an 'id' for undefined in an object passed to 'push'

从那个错误中,我觉得它正确地从我的 api 调用中获取了“文章”部分,但 primaryKey 部分似乎没有生效。

如果我删除我的序列化程序的内容,我会得到以下错误列表:

ember.debug.js:7062WARNING: Encountered "embedded" in payload, but no model was found for model name "embedded" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("embedded"))
ember.debug.js:7062WARNING: Encountered "page_count" in payload, but no model was found for model name "page-count" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page_count"))
ember.debug.js:7062WARNING: Encountered "page_size" in payload, but no model was found for model name "page-size" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page_size"))
ember.debug.js:7062WARNING: Encountered "total_items" in payload, but no model was found for model name "total-item" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("total_items"))
ember.debug.js:7062WARNING: Encountered "page" in payload, but no model was found for model name "page" (resolved model name using ember-drupal@serializer:application:.modelNameFromPayloadKey("page"))

让我知道是否需要包含任何其他信息/代码。

编辑

注意:API 开发人员确实将其从“nid”更改为“id”

有时你只需要从头开始,对吧?

这就是我的序列化程序现在的样子,它按预期工作:

export default DS.RESTSerializer.extend({
    normalizeResponse(store, primaryModelClass, payload, id, requestType) {
        payload = { article: payload._embedded[primaryModelClass.modelName] };
        return this._super(store, primaryModelClass, payload, id, requestType);
    },
});
4

0 回答 0