Ember JS : 如何在调用时重用 ember 数据?

Ember JS : How to reuse ember data while calling?

我有两个模型要调用。在第一个模型中,我包含了一些像这样的日期。

this.store.query('comment',{
   include : 'person,address'
});

在第二次调用中,我添加了已存储在商店中的相同详细信息。

this.store.query('post',{
   include : 'person,address'
});

因此,API 调用需要很长时间才能解决。有什么方法可以使用第一个 API 在第二个 API 调用中包含数据来创建这两个模型(人、地址)之间的关系。 这会为我节省很多时间。

注意:示例仅供测试。

您正在使用 query() method of Ember Data's store. It expects two arguments: the model name as first argument and the query as a second argument. Last one is directly passed to your backend as part of the request. The responsible code is quite simple: https://github.com/emberjs/data/blob/v3.10.0/addon/adapters/rest.js#L535-L560

如果您使用默认 JSONAPIAdapter,您的方法调用执行的请求如下所示:

this.store.query('comment', { include: 'person,address' });
=> GET /comments?include=person,address

this.store.query('post', { include: 'person,address' });
=> GET /posts?include=person,address

API 从该请求中不知道客户端已经在本地缓存了一些 personaddress 记录。 Ember 默认情况下,数据不包含该信息。您可以自定义您的适配器两次,但我不建议这样做 - 特别是因为这可能会增加请求大小并降低缓存命中率相当大的数量。您可能还想重新加载本地缓存的记录。

如果您希望两个已经在本地缓存了大部分相关记录,您可能根本不想让服务器包含它们?在那种情况下,之后将它们加载到 coalesced request.

中可能更便宜