gpt4 book ai didi

javascript - Ember.js 更改 RESTAdapter JSON 序列化

转载 作者:行者123 更新时间:2023-11-28 09:11:44 25 4
gpt4 key购买 nike

我有一个正在从 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

最佳答案

您想要继承 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);
}
},

由于您的服务器未将类型设置为根,因此您无论如何都无法使用侧面加载。

关于javascript - Ember.js 更改 RESTAdapter JSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16244108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com