简体   繁体   English

在角度服务中处理来自后端api的数据的最佳方法是什么?

[英]What is the best way to work with data from a backend api in an angular service?

I'm having trouble understanding the best way to work with and handle data from a backend api in an angular2+ service. 我无法了解在angular2 +服务中使用和处理后端api数据的最佳方法。

For example when creating a cart system. 例如,在创建购物车系统时。 I initially send a get request to the backend to get the current cart. 我最初将get请求发送到后端以获取当前购物车。 This is where I start having difficulties, when it comes to working with this data. 在处理这些数据时,这就是我开始遇到困难的地方。

What is the best way to update the data? 什么是更新数据的最佳方法? If the method to update the data just involves sending a put/post request to the backend - my components that are subscribed to the data would not be updated. 如果更新数据的方法仅涉及向后端发送放置/发布请求-预订数据的组件将不会被更新。 If I include in that logic another get request to get the updated data, theres a chance that the get request will return before my backend logic is able to update the value to the db and return the updated value. 如果我在该逻辑中包含另一个get请求以获取更新的数据,则在我的后端逻辑能够将值更新到db并返回更新的值之前,获取请求可能会返回。 There are other options available to me, like using Subjects , but those too have their issues. 我还可以使用其他选项,例如使用Subjects ,但是这些选项也存在问题。

This seems like an obvious and common task so I'm not sure if there's something obvious that I'm missing. 这似乎是一项显而易见的常见任务,所以我不确定是否缺少明显的东西。

If you were to create a cart system, or a like system, or anything that gets an initial value from a backend and needs to be updated, how would you handle that data - what would be your workflow? 如果要创建购物车系统或类似系统,或者从后端获取初始值并需要更新的任何系统,您将如何处理该数据-您的工作流程是什么?

Don't see this as the best way but just a way: 不要认为这是最好的方法,而只是一种方法:

Getting the data is done on some user action - eg entering a page or pressing the refresh button. 获取数据是通过某些用户操作完成的,例如,进入页面或按下刷新按钮。 Doing so will allow the end user to view/interact with data. 这样做将允许最终用户查看/交互数据。

When updating data, send data to the server and if it returns a success response (and it should respond in some way that the operation is acknowledged on the server side) then treat the current client data as the current state of that data, so no additional get request. 更新数据时,将数据发送到服务器,如果它返回成功响应(并且应该以某种方式响应,即在服务器端确认了该操作),则将当前客户端数据视为该数据的当前状态,因此额外的获取请求。 On error response or timeout handle as needed. 根据需要提供错误响应或超时句柄。

If there is a need from the server to update or change the sent data, then the server could send that change in the response to the update request (Id for example) or it could send some information about when it will be possible to get the correct state of the data (timespan for example). 如果服务器需要更新或更改已发送的数据,则服务器可以在对更新请求的响应中发送该更改(例如,Id),或者可以发送有关何时可以获取证书的信息。数据的正确状态(例如时间跨度)。

If there is a possibility that some other source will edit the data on the server and that happens after you received the old data but before you update it, then you could return an error response that will warn the user that the data is changed and it needs to be refreshed (manually or by user clicking the OK button on the warning) after which you send a get request to the server again. 如果有其他来源可能会编辑服务器上的数据,并且这种情况是在您收到旧数据之后但在更新之前发生的,那么您可能会返回错误响应,警告用户数据已更改并且需要刷新(手动或通过用户单击警告上的“确定”按钮),然后将获取请求再次发送到服务器。

It could be seen as bad user experience if the user updates some data and is presented with data that is changed on some other source and not with the edits he made. 如果用户更新了一些数据,并且向用户显示了在其他来源上更改过的数据,而不是他所做的编辑,则这可能会视为不良的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM