如何序列化ember分页数据

时间:2016-10-05 14:16:03

标签: ember.js ember-data ember-cli ember-cli-addons ember-cli-rails

我从我的其余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 Book Model

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

Ember Route

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:无法读取属性' type'未定义的

我正在使用以下版本的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)

1 个答案:

答案 0 :(得分:0)

你应该checkout the documentation

我认为你必须实施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数组中提取数据。