[英]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.