持久保存js用户控制状态
我的 RIA 应用程序有很多 js UI 控件(几乎所有都是 jQuery UI 部分,如 datepicker、dialog、jqgrid)。因此,然后用户在一个页面上使用一些控件,然后转到另一个页面,然后单击返回所有页面组件都具有初始状态(文本框为空,网格为空等)。那么如何保留 UI 控件状态,然后在页面之间恢复它呢?看来我需要一些类似 JS 序列化/反序列化方法在用户服务器会话中存储序列化数据。我怎样才能以最小的成本做到这一点?你在项目中是如何做到这一点的?任何想法、链接、帖子将不胜感激。提前谢谢你们了!
PS 我的项目是 ASP .NET MVC3
编辑
现在我记得 memnto 设计模式。你们中有人能给我一些与这个想法相关的建议吗? 再次感谢!
My RIA application has a lot js UI controls (almost all of their is jQuery UI parts like datepicker, dialog, jqgrid). So then user works with some controls on one page and then go to another page and then clicks back all page components has initial state (textboxes are empty, grids are empty and so on). So how can I persist UI controls state and then restoring it between pages? It seems I need some like JS serialization/deserialization methods storing serialized data in user server session. How can I do it with minimal costs? How do you guys it in your projects? Any thougs, links, posts will be very appreciated. Thank you guys in advance!
P.S. My project is ASP .NET MVC3
EDIT
Just now I remember about memnto design pattern. Could anyone of you advice me something related to this idea?
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我写这篇文章是对你的问题的评论,但我将其作为答案,因为这可能是做到这一点的唯一方法,而不必使用某种侧面插件“破解”它。
Viewstate 的使用与您是否使用 JQuery UI“插件”无关。您所要做的就是使用服务器端控件。
请注意,为此您必须使用控件客户端名称,例如:
这样您就可以在服务器端控件上应用 JQuery UI,并利用 Viewstate 在后退按钮导航上恢复控件值。
I wrote this as a comment of your question but I'm putting it as an answer as it might be the only way to do this without having to "hack" it with some sort of side plugins.
The use of Viewstate have nothing to do with you using or not JQuery UI "addins". All you have to do is use server-side controls instead.
Note that for this you have to use the control client-side name, something like:
This way you can apply JQuery UI on server-side controls and take advantage of the Viewstate restoring the controls value on back-button navigation.
我会使用 Persist-JS (GitHub 上的 Persist-JS )、jQuery(您已经在使用)和 json.js (JSON-js在GitHub)。
像这样:
并且...
换句话说,循环遍历页面上的所有输入,存储它们的值 (
$.val()
),创建一个 JSON 对象并将其保留为这些值。恢复表单时,只需执行相反的操作 - 循环输入并按名称formData[name]
从 JSON 对象中获取属性。那里有一些防御性的东西(即:检查持久化对象是否为空,这就是 persist-js 的工作原理)。I would do it using Persist-JS (Persist-JS on GitHub), jQuery (which you are using already) and json.js (JSON-js on GitHub) .
Something like this:
and...
In other words, loop through all the inputs on the page, store their value (
$.val()
), create a JSON object and persist it for these values. When restoring the form, simply do the opposite -- loop through the inputs and grab the properties off of the JSON object by nameformData[name]
. There's some defensive stuff in there (ie: check for persisted object to be null, which is how persist-js works).