mardi 5 décembre 2017

Ember.js - change the format of a network request sent when getting a model's related objects

I am trying to make my network calls between my Ember app and my Rails API backend more efficient when fetching an ember data object's related objects. Let's say I have two models:

  • shop - a shop has many products
  • product - a product belongs to a shop

I am using a RESTAdapter for both of these models, so if I try to get a shop's products like so:

let shop = this.store.find("shop", 1);
let products = shop.get("products");

Let's assume I have a shop with a hasMany relationship to the first 500 products in my database. The network requests will look something like this (assume all url paths are valid and lead to valid controllers and actions):

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
...

My products api endpoint can take in a filter parameter to feed into an ApplicationRecord.where call. So what I'd really like to do is to be able to change the outcome of the shop.get("products") call so it makes a single call to my products endpoint with a filter parameter and gets all related products in bulk, instead of 500 individual calls to the api endpoint.

Is there an established pattern for doing this?




Aucun commentaire:

Enregistrer un commentaire