简体   繁体   English

Ajax和本地/会话存储模式

[英]ajax and local/session storage pattern

I'm building a web app that uses ajax to communicate with the server. 我正在构建一个使用ajax与服务器通信的Web应用程序。 Basically, the user requests a record, it comes back in json, it's added to the DOM and the user makes changes to it. 基本上,用户请求一条记录,它以json形式返回,并被添加到DOM中,并且用户对其进行更改。 When the user requests the next record, the current record is stringified and sent back to the server and the following record comes back. 当用户请求下一条记录时,当前记录将被字符串化并发送回服务器,随后的记录将返回。

All this works really well.... as long as the user keeps requesting records. 只要用户不断请求记录,所有这些工作都很好。 However, I am wondering how to handle the situation where the user stops his work: how do I get the last record updated? 但是,我想知道如何处理用户停止工作的情况:如何更新最近的记录?

I thought of adding the working record to the local storage while he's editing it and at each edit, updating the local storage and if he logs on next time and there's still a record in there, ajax it when he logs on. 我想在他编辑工作记录时以及每次编辑时将工作记录添加到本地存储中,以更新本地存储,如果他下次登录并且那里仍然有记录,则在他登录时将其记录为ajax。 The problem with his approach is that if another user logs on to the same computer, then when that new user logs on, he's updating the data of another user. 他的方法的问题在于,如果另一个用户登录到同一台计算机,则当该新用户登录时,他正在更新另一个用户的数据。

I thought of using the window.unload event also; 我还考虑过使用window.unload事件; but that doesn't solve the problem of the user closing his browser before the final update. 但这不能解决用户在最终更新之前关闭浏览器的问题。

What are some good ways to handle this issue. 有什么好的方法可以解决此问题。 Thanks for your suggestions. 感谢您的建议。

If your app requires login, you could key the localStorage using their ids like so: 如果您的应用程序需要登录,则可以使用其ID键入localStorage,如下所示:

localStorage.getItem( "user13434" )

would retrieve data for user13434 将为用户13434检索数据

localStorage.getItem( "user12345" )

would retrieve data for user12345 会为user12345检索数据

If the information is sensitive but not too sensitive you could add encryption, but it can be decrypted by experienced users which is why it must not be too sensitive. 如果信息敏感但不太敏感,则可以添加加密,但是有经验的用户可以对其进行解密,这就是为什么它不能太敏感的原因。

I would consider a 'draft-like' feature. 我会考虑“草稿式”功能。 Where you could upload changes after a certain amount of time of no input, for instance, after 15 seconds of no input, push those changes. 在经过一定时间的无输入后可以上传更改的位置,例如,在无输入15秒钟后,将这些更改推送。

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

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