【发布时间】:2014-05-13 15:27:14
【问题描述】:
我有一个每次返回一个资源的 API。
例如,假设我有两个相关资源:
TopicsPosts
Topics 拥有许多Posts,而Posts 属于Topic。
根据文档(以及我尝试和阅读的所有内容),Ember-Data 期望 API 中的 JSON 类似于以下内容,其中Topic 的数据和Posts 同时提供。
"topic": {
"id": ...,
"title": ...,
"author": ...
},
"posts": [{
"id": ...,
"commenter": ...,
"text": ...
}, {
"id": ...,
"commenter": ...,
"text": ...
}]
正如我在文章开头提到的,我的 API 没有这样做。它将在一次调用中返回Topic 数据,并在第二次调用中返回Topic 的Posts 数据。更改我的 API 以完全按照 Ember 的要求工作不是一种选择。
我的问题是,如何扩展/覆盖 Ember-Data 中的 RESTAdapter 和 RESTSerializer 以支持从多个 API 调用加载数据?这甚至可能吗?
我尝试按照这个示例进行操作,但看起来这里的数据已经加载(它试图旁加载系统已知的数据):http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/
我还尝试覆盖ApplicationSerializer 的extractFindAll 函数,但不知道如何让应用程序的其余部分(不是双关语)等待所有关系加载。我遍历模型上的所有关系,并加载它们的数据,但我看不到将这些数据传送回原始有效负载的方法:
extractFindAll: function (store, type, payload, id, requestType) {
var promises = [];
type.eachRelationship(function (key, relationship) {
promise.push(Ember.RSVP.Promise(function (resolve, reject) {
this.store.find(key, payloadEntry[key]);
}));
});
// can't figure out how to get the relationship payloads
// back to the original payload
Ember.RSVP.addSettled(promises).then(function () {
return this._super(store, type, payload, id, requestType);
});
}
感谢任何帮助。
【问题讨论】:
标签: javascript ember.js ember-data