1

假设我有一个 JSON API,我可以在其中访问两个模型:catsdogs. 但是,在我的 Ember 应用程序中,我只有一个模型:animals.

虽然每次我都save()animal它作为 POST 到 API cat,但如果我收到dog任何其他模型中的链接,它应该在本地存储为animalember-data。

实现这一目标的最连贯的方式是什么?谢谢!

4

2 回答 2

1

固定的。为了将来参考,可以为模型创建一个别名,扩展 ApplicationSerializer(在这种情况下,我们的模型是animal,虽然它有一个cat在 API 中使用模型的适配器,但dog需要将模型解析animal为出色地):

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
  typeForRoot: function(root) {
    if (root == 'dog' || root == 'dogs') { root = 'animal'; }
    return this._super(root);
  }
);
于 2015-02-12T15:59:11.957 回答
0

您应该能够通过自定义 REST 适配器来实现您想要的。具体来说,考虑重写该buildURL方法以检测传递的记录的类型,并根据确定此特定模型是否应持久保存到cats端点或dogs端点的逻辑来构造要传递的 URL。

于 2015-02-12T15:32:15.943 回答