0

Lets say we are working on a recipe app and we have 2 get requests initially. /ingredients/ /recipes/ These create the recipe and the ingredient model. Now recipes are made of ingredients and amount of each ingredient. And ingredients only have names.

Now populating the models are easy using the models and the default serializes and the adapters if we confirm to the https://jsonapi.org/

How do we handle when one of the ingredients is deleted. In the backend I use django if and ingredient is deleted DB also deletes all the recipes that have relation to the that ingredients.

But on the SPA application the let say we ran

ingredient.deleteRecord();
  ingredient.get('isDeleted'); // => true
  ingredient.save();

or

ingredient.destroyRecord();

How does the recipes model get updated ? So do you manually do what server does on the client(and delete related recipes by custome code)? Or should you just update the list and repopulate it(easier). And can you give dependencies between models so a model in the store would know that it need to be updated when one of it dependencies model is changed so it automatically make a get request and repopulate itself(are routes/models that clever?)

On that note what is the standard way of keeping server and client in sync.

What is the standard for this sort of stuff. Should client app re-get all that data again just to be sure that server and spa are insync and uptodate.

Thanks

Evren Bingøl
  • 1,306
  • 1
  • 20
  • 32

1 Answers1

0

In the scenario where an ingredient gets deleted, the save has gone through successfully on the backend and now needs that ingredient needs to be removed from many recipes on the frontend before your UI is truly in sync, I would suggest using this gist to push your delete properly into the store: https://gist.github.com/runspired/96618af26fb1c687a74eb30bf15e58b6

acorncom
  • 5,975
  • 1
  • 19
  • 31
  • 1
    alternatively, if the recipe has a many relationship with ingredients it will automatically be removed from that relationship on the client once the delete is persisted. – runspired Sep 03 '22 at 04:44