1

我有一个从 API 查询的模型:

App.Deal = DS.Model.extend({
  name: DS.attr('string'),
  value_in_cents: DS.attr('number'),
  closed_time: DS.attr('date'),
  user: DS.attr('object'),
  company: DS.attr('object')
});

API 返回如下内容:

{ 
  pagination: { page: 1, total: 10 },
  entries: [ deal1, deal2, deal3, deal4 ]
}

我已将我的适配器更新为以下内容:

App.Adapter = DS.RESTAdapter.extend({
  url: 'http://api.pipelinedeals.com/api/v3',
  serializer: DS.RESTSerializer.extend({
    extractMany: function(loader, json, type, records) {
      var root = this.rootForType(type);
      var roots = this.pluralize(root);

      formattedJson = {};
      formattedJson[roots] = json.entries;
      delete formattedJson.pagination;
      this._super(loader, formattedJson, type, records);
    }
  })
});

newJson有我想要的格式,但我收到以下错误:

Uncaught Error: assertion failed: You tried to use a attribute type (object) that has not been registered ember-1.0.0-rc.2.js:52
4

1 回答 1

0

你想继承DS.RESTSerializer和覆盖extractMany有这样的东西:

extractMany: function(loader, json, type, records) {
    var root = 'entries';

    if (json[root]) {
      var objects = json[root], references = [];
      if (records) { records = records.toArray(); }

      for (var i = 0; i < objects.length; i++) {
        if (records) { loader.updateId(records[i], objects[i]); }
        var reference = this.extractRecordRepresentation(loader, type, objects[i]);
        references.push(reference);
      }

      loader.populateArray(references);
    }
  },

由于您的服务器没有将类型设置为 root,因此您无论如何都无法使用侧载。

于 2013-04-27T23:03:22.163 回答