【发布时间】:2013-04-26 19:47:07
【问题描述】:
我有一个从 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
【问题讨论】:
-
您确定
var roots = this.pluralize(root); // deals将真正的“交易”作为字符串返回吗? -
您不想将
DS.RESTAdapter与DS.JSONSerializer一起使用。它不会起作用(或者你会在某些时候出现意想不到的行为)。 -
查看其他 SO 问题 stackoverflow.com/questions/16037175/… 以自定义您的序列化程序。
-
是的,根按预期工作。有道理,我将改用 DS.JSONSerializer。通过覆盖
extract方法,我现在收到以下错误:未捕获错误:断言失败:您的服务器返回了带有键分页的哈希,但您没有映射它 ember-1.0.0-rc.2.js:52 Ember.assert ember-1.0.0-rc.2.js:52 DS.JSONSerializer.DS.Serializer.extend.sideload ember-data.js:6602 DS.JSONSerializer.DS.Serializer.extend.extractMany ember-data.js :6546 superWrapper ember-1.0.0-rc.2.js:947 -
另外,这个 SO 帖子 (stackoverflow.com/questions/16037175/…) 覆盖了
extract方法,而extractMany出现在它之前,所以我收到了this.sideload中失败的错误 b/c。还有其他建议吗?
标签: javascript ember.js ember-data