您可以延長DS.RESTSerializer
並更改extract
和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負載加載到您的應用程序中。
(見fiddle)
謝謝你,這是我一直在尋找的答案。 – 2013-05-01 18:31:06