我的rails应用程序用于生成如下所示的JSON:
{"paintings":
[
{"id":1,"name":"a"},
{"id":2,"name":"b"}
]
}
我添加了rabl json格式,现在json看起来像这样:
[
{"id":1,"name":"a"},
{"id":2,"name":"b"}
]
Ember告诉我
Uncaught Error: assertion failed: Your server returned a hash with the key 0 but you have no mapping for it
如何让Ember明白这一点?或者我应该如何让兔子理解为可以理解?
答案 0 :(得分:3)
找到解决方案。我的index.json.rabl看起来像这样:
collection @paintings
extends 'paintings/show'
现在看起来像这样:
collection @paintings => :paintings
extends 'paintings/show'
答案 1 :(得分:1)
您可以延长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)