ajax 和本地/会话存储模式
我正在构建一个使用 ajax 与服务器通信的网络应用程序。基本上,用户请求一条记录,它以 json 形式返回,它被添加到 DOM 中,然后用户对其进行更改。当用户请求下一条记录时,当前记录被字符串化并发回服务器,并返回下一条记录。
所有这些都非常有效......只要用户不断请求记录。但是,我想知道如何处理用户停止工作的情况:如何更新最后一条记录?
我想到在他编辑工作记录时将工作记录添加到本地存储中,并且在每次编辑时更新本地存储,如果他下次登录并且那里仍然有记录,则在他登录时使用ajax它。他的方法的问题在于,如果另一个用户登录到同一台计算机,那么当该新用户登录时,他正在更新另一个用户的数据。
我也想过使用 window.unload 事件;但这并不能解决用户在最终更新之前关闭浏览器的问题。
有哪些好的方法可以处理这个问题。感谢您的建议。
I'm building a web app that uses ajax to communicate with the server. Basically, the user requests a record, it comes back in json, it's added to the DOM and the user makes changes to it. 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. 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; 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑“类似草稿”的功能。您可以在一定时间没有输入后上传更改,例如,15 秒没有输入后,推送这些更改。
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.
如果您的应用需要登录,您可以使用 localStorage 的 ID 进行键入,如下所示:
localStorage.getItem( "user13434" )
将检索 user13434 的数据
localStorage.getItem( "user12345" )
将检索 user12345 的数据
如果信息敏感但不太敏感,您可以添加加密,但经验丰富的用户可以解密,这就是为什么它必须不要太敏感。
If your app requires login, you could key the localStorage using their ids like so:
localStorage.getItem( "user13434" )
would retrieve data for user13434
localStorage.getItem( "user12345" )
would retrieve data for 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.