简体   繁体   English

Angular等待后端更新响应时该怎么办?

[英]Angular what to do when waiting for response for backend updates?

In the tutorial they have an example app where users can upvote things. 在本教程中,他们有一个示例应用程序,用户可以在其中投票。 When the user clicks upvote it makes a request to update the backend and it waits until this request is successful before it displays the effect to the user. 当用户单击upvote时,它将发出更新后端的请求,并等待该请求成功,然后才向用户显示效果。 The code is as follows: 代码如下:

$scope.incrementUpvotes = function(post) {
  posts.upvote(post);
};
o.upvote = function(post) {
  return $http.put('/posts/' + post.id + '/upvote.json')
    .success(function(data){
      post.upvotes += 1;
    });
};

The downside of this is that if you have slow internet there could be delay which would be a bad user experience. 这样做的缺点是,如果互联网速度较慢,则可能会出现延迟,这将带来糟糕的用户体验。

The two options I see would be to either show a loading symbol to the user until it's successful. 我看到的两个选项是向用户显示加载符号,直到成功为止。 Show the effect of the upvote immediately while the backend sync happens in the background. 当后台同步发生在后台时,立即显示upvote的效果。 The danger of this is if the request fails then the data between client and server will not be in sync. 这样做的危险是,如果请求失败,则客户端和服务器之间的数据将不会同步。

I've also seen some people use an ajax queue like as follows: 我还看到有些人使用ajax队列,如下所示:

Add queueing to angulars $http service 将队列添加到angulars $ http服务

EDIT for an actual question: 编辑一个实际问题:

What is the best practice here in terms of development and user experience? 在开发和用户体验方面,最佳实践是什么? What do most people do and are there any pros/cons that I should be aware of? 大多数人会做什么,我应该注意哪些利弊?

With a few of my project I have done this for desirably fast UX. 在我的一些项目中,我已经实现了理想的快速用户体验。 I have used both your examples, however if I use your second example, I include an error catch to resync the back and front end, notifying the user of the error. 我已经使用了两个示例,但是如果使用第二个示例,则会包含一个错误捕获以重新同步后端和前端,从而将错误通知用户。 For example: 例如:

o.upvote = function(post) {
    post.upvotes += 1;
    return $http.put('/posts/' + post.id + '/upvote.json')
        .success(function(data){
            // don't have to do anything here
        })
        .catch(function(err){
            post.upvotes -= 1;
            // inform the user of the error
        });
};

My general rule of thumb is to use the first option if the interaction is very important, or the action is a prerequisite for a further action. 我的一般经验法则是,如果交互非常重要,或者该操作是采取进一步操作的先决条件,则使用第一个选项。 I use the second option for independent events that the user expects to work quickly and seamlessly. 我将第二个选项用于用户希望快速无缝地工作的独立事件。

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

相关问题 tap 不等待来自后端服务调用的响应 - tap is not waiting for response from backend service call in angular 在 Angular HTML 中显示后端响应 - Display backend response in Angular HTML 在Angular 2中,response.json()有什么作用? - In Angular 2 what does response.json() do? 在等待后端响应时快速更改可观察的 state - Alter observable state quick while waiting for backend response 如何在Angular中实现SYNC方法,等待响应,没有订阅,就像C#中的SYNC方法一样? - How do I implement a SYNC method in Angular, waiting for response, with no subscribe, like SYNC methods in C#? 如何使 API 响应在 angular 中自动更新 - How to make API response updates automatically in angular 等待异步方法完成时,如何正确发出对帖子的响应? - How do I properly issue response to post when waiting for async method to complete? 在等待$ onInit回调完成时,我为Angular binded属性返回什么? - What do I return for an Angular binded property while waiting for $onInit callbacks to complete 如何在 Angular 中处理 LocaleDateTime 后端响应 - How to handle LocaleDateTime backend response in Angular 如何编写仅当用户与UI中的下拉列表进行交互而不在后端进行模型更新时才执行的函数? - How do I write a function that will get executed only when the user interacts with the dropdown in the UI but not on model updates from backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM