Ember.js - 更改获取模型相关对象时发送的网络请求的格式
Ember.js - change the format of a network request sent when getting a model's related objects
我正在尝试使我的 Ember 应用程序和 Rails API 后端之间的网络调用在获取 ember 数据对象的相关对象时更有效率。假设我有两个模型:
shop
- 一个 shop
有多个 products
product
- product
属于 shop
我正在为这两种型号使用 RESTAdapter
,所以如果我尝试像这样购买商店的产品:
let shop = this.store.find("shop", 1);
let products = shop.get("products");
假设我有一个 shop
与我数据库中的前 500 个 product
有 hasMany
关系。网络请求看起来像这样(假设所有 url 路径都有效并导致有效的控制器和操作):
localhost:3000/api/v1/shops/1
localhost:3000/api/v1/products/1
localhost:3000/api/v1/products/2
localhost:3000/api/v1/products/3
localhost:3000/api/v1/products/4
localhost:3000/api/v1/products/5
...
我的 products
api 端点可以接受一个 filter
参数以馈入 ApplicationRecord.where
调用。所以我真正想做的是能够更改 shop.get("products")
调用的结果,以便它使用过滤器参数对我的 products
端点进行一次调用并批量获取所有相关产品,而不是对 api 端点进行 500 次单独调用。
是否有执行此操作的既定模式?
是的,有一个 pattern/a 参数。
它称为 coalesceFindRequests
并将在您的应用程序适配器中设置:
// app/adapters/application.js
export default DS.RESTAdapter.extend({
coalesceFindRequests: true,
// more params to be set here
});
我正在尝试使我的 Ember 应用程序和 Rails API 后端之间的网络调用在获取 ember 数据对象的相关对象时更有效率。假设我有两个模型:
shop
- 一个shop
有多个products
product
-product
属于shop
我正在为这两种型号使用 RESTAdapter
,所以如果我尝试像这样购买商店的产品:
let shop = this.store.find("shop", 1);
let products = shop.get("products");
假设我有一个 shop
与我数据库中的前 500 个 product
有 hasMany
关系。网络请求看起来像这样(假设所有 url 路径都有效并导致有效的控制器和操作):
localhost:3000/api/v1/shops/1
localhost:3000/api/v1/products/1
localhost:3000/api/v1/products/2
localhost:3000/api/v1/products/3
localhost:3000/api/v1/products/4
localhost:3000/api/v1/products/5
...
我的 products
api 端点可以接受一个 filter
参数以馈入 ApplicationRecord.where
调用。所以我真正想做的是能够更改 shop.get("products")
调用的结果,以便它使用过滤器参数对我的 products
端点进行一次调用并批量获取所有相关产品,而不是对 api 端点进行 500 次单独调用。
是否有执行此操作的既定模式?
是的,有一个 pattern/a 参数。
它称为 coalesceFindRequests
并将在您的应用程序适配器中设置:
// app/adapters/application.js
export default DS.RESTAdapter.extend({
coalesceFindRequests: true,
// more params to be set here
});