Problem description:

Parent view displays a list of items. This view has button add a new item to list wich open modal for creating a new item.

Two approaches how to do that:

1) POST Request -> Insert from response body new list item to the parent view

2) POST Request -> Notify parent view that new item created -> Parent view will do GET request which returns the full list of items and view rerenders

The first approach is commonly used. But the second approach gives some feeling of overkill. The first approach uses one request to the server, but the second two request to the server.

It is ok after POST request notify parent view to create a GET request to get all items that update view after creating? If it is ok when is it ok to use?

  • @Laiv Yes, for example, if the parent somehow can subscribe to HTTP client response. As it is possible to do in Angular with HttpClient. – Stanislav Machel May 16 '19 at 13:40
  • What's normal, best or better depends on your specific needs. You will have to weight the pros and cons of each approach from the standpoint of the functional and non-functional requirements. As for the child notifying the parent, avoid the childs aware of the parent if possible (to break cyclic dependencies and possible memory leaks). If you can pass callbacks down the hierarchy tree do it that way. – Laiv May 16 '19 at 13:41

The problem is probably not the number of requests but that in the second approach adding a single item will result in a complete GET for and rerender of all items.

I suggest a

Third approach: The response to the POST request contains information needed to identify the new item. The client executes a GET request to get the details of the new item. It inserts a new row into the view.

  • Do you mean a situation of using the third approach when data which return POST request is not enough for displaying item in parent view? But is the second approach ok when data from POST is enough for displaying in parent view? – Stanislav Machel May 16 '19 at 13:14
  • Regarding your first question: Yes, if the response only contains information which identifies the item but not every detail, approach three could be used. If the response already contains enough information to insert the item into the view, approach one could be used. – user330854 May 16 '19 at 13:19
  • Regarding your second question: In my understanding the response only contains information about the new item, not about the other items. – user330854 May 16 '19 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.