您可以扩展DS.RESTSerializer
和更改extract
and extractMany
。以下只是我在 .NET 中使用的序列化程序的复制和粘贴,用于相同的场景:
window.App = Ember.Application.create();
var adapter = DS.RESTAdapter.create();
var serializer = Ember.get( adapter, 'serializer' );
serializer.reopen({
extractMany: function (loader, json, type, records) {
var root = this.rootForType(type);
root = this.pluralize(root);
var objects;
if (json instanceof Array) {
objects = json;
}
else {
this.sideload(loader, type, json, root);
this.extractMeta(loader, type, json);
objects = json[root];
}
if (objects) {
var 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);
}
},
extract: function (loader, json, type, record) {
if (record) loader.updateId(record, json);
this.extractRecordRepresentation(loader, type, json);
}
});
在设置商店之前,您必须将模型配置为正确侧载:
serializer.configure( 'App.Painting', {
sideloadAs: 'paintings'
} );
App.Store = DS.Store.extend({
adapter: adapter,
revision: 12
});
现在您应该能够将无根 JSON 有效负载加载到您的应用程序中。
(见小提琴)