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