开发者

Ajax patterns - assume success or wait for response

Typically an ajax interaction would involve sending the request, providing feedback to the user that the request is in process, then once the response arrives handle it.

Waiting for the response is obviously unavoidable when the next action requires the data sent from the server but what if the interaction is an update to the some data on the server such as sorting the order of a list. Would it be bad practice to assume success? So you'd make the request and simply update the DOM based on the assumption that the sort will be successful. I'd imagine you'd then have to provide a rollbac开发者_JAVA技巧k method should the request fail as well as notify the user but 99% of the time the request should go through and appear instantaneous to the user.

Is this a common pattern and are there any other factors that should be considered other than a rollback and notification method?

Any advice would be much appreciated, Rich


You probably want to take a look at the command pattern http://en.wikipedia.org/wiki/Command_pattern. It looks like you want to modify some data and assume that the server gets modified as well. If the AJAX handler fails than you can rollback the command (and notify the user).


If the user is to receive feedback about the success or not, then it would be bad practice to assume success.

Just return a success/fail response from the server, and let the user know that their action was successful. You're not really losing anything by this extra trip back from the server, and any request could potentially fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜