0

我从我的其余 API 中获取以下 json:

{
  "content": [{
    "id": 56789,
    "name": "sample book",
    "status": "available"
  },{
    "id": 56789,
    "name": "sample book",
    "status": "available"
  }],
  "last": true,
  "totalElements": 5,
  "totalPages": 2,
  "size": 3,
  "number": 1,
  "sort": null,
  "first": false,
  "numberOfElements": 2
}

Ember 图书模型

export default DS.Model.extend({
  id: DS.attr(),
  name: DS.attr(),
  status: DS.attr()
});

余烬路线

import Ember from 'ember';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';

export default Ember.Route.extend(RouteMixin, {

  model: function(params) {
      params.paramMapping = {
        perPage: "size"
      };
      return this.findPaged('book',params);
  }
});

在 Firefox 网络选项卡中,我可以看到响应,但在浏览器控制台中,我收到以下消息:

处理路由时出错:book.index 数据未定义 _pushInternalModel

在 Chrome 中出现以下异常:

ember.debug.js:30610 处理路由时出错:books.index 无法读取未定义的属性“类型”TypeError:无法读取未定义的属性“类型”

我正在使用以下版本的 ember:

  • "ember-cli": "2.5.0",
  • "ember-cli-pagination": "2.2.2"

我认为我应该使用“normalizeResponse”序列化响应,但我不知道该怎么做。

根据Lux评论更改序列化程序:

import JSONSerializer from 'ember-data/serializers/json';
import DS from'ember-data';

export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
    product: {embedded: 'always'}
},
extractMeta(store, typeClass, payload) {
  let meta = [{
  "last": payload.last,
  "totalElements": payload.totalElements,
  "totalPages": payload.totalPages,
  "size": payload.size,
  "number": payload.number,
  "sort": payload.sort,
  "first": payload.first,
  "numberOfElements": payload.numberOfElements
}];
delete payload.content;
delete payload.last;
delete payload.totalElements;
delete payload.totalPages;
delete payload.size;
delete payload.number;
delete payload.sort;
delete payload.first;
delete payload.numberOfElements;

return meta;
},
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
let documentHash = {
  data: [],
  included: [],
  meta: this.extractMeta(store, primaryModelClass, payload),
};
payload.content.forEach(item => {
  let { data, included } = this.normalize(primaryModelClass, item);
if (included) {
  documentHash.included.push(...included);
}
documentHash.data.push(data);
});
}
});

收到以下错误:

ember.debug.js:19750 TypeError: Cannot read property 'forEach' of undefined
at Class.normalizeArrayResponse (device.js:41)
at Class.normalizeQueryResponse (json.js:333)
at Class.normalizeResponse (json.js:228)
4

1 回答 1

0

您应该查看文档

我认为您必须实施extractMeta

extractMeta(store, typeClass, payload) {
  let meta = [{
    "last": payload.last,
    "totalElements": payload.totalElements,
    "totalPages": payload.totalPages,
    "size": payload.size,
    "number": payload.number,
    "sort": payload.sort,
    "first": payload.first,
    "numberOfElements": payload.numberOfElements
  }];
  delete payload.content;
  delete payload.last;
  delete payload.totalElements;
  delete payload.totalPages;
  delete payload.size;
  delete payload.number;
  delete payload.sort;
  delete payload.first;
  delete payload.numberOfElements;

  return meta;
}

included是其他包含的记录。分页数据等信息应该在meta散列中。所以 ember 认为您向它提供了额外的记录,但找不到它们的类型。

接下来你实现normalizeArrayResponse. 可能这样的事情会为你工作:

normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
  let documentHash = {
    data: [],
    included: [],
    meta: this.extractMeta(store, primaryModelClass, payload),
  };

  payload.content.forEach(item => {
    let { data, included } = this.normalize(primaryModelClass, item);
    if (included) {
      documentHash.included.push(...included);
    }
    documentHash.data.push(data);
  });
}

这是从content数组中提取数据。

于 2016-10-05T22:08:46.640 回答