使用 localStorage 在 SenchaTouch/PhoneGap -App 中保存/加载设置并以表单显示它们
当您想使用 localStorage 时,应该如何在应用程序中保存/加载设置? 假设我们只想保存用户名和密码。
目前我知道两个选择:
- 使用带有存储和代理的模型。
这似乎有点太大了,因为您只想存储一个对象。但您有一个优势,可以在设置 FormPanel 中轻松使用 this.load() 。 - 使用 Phonegap 或“普通”localStorage。
然后你必须使用 JSON.stringify 和 JSON.parse 但你只存储一个对象并且更容易加载它(页面刷新后我在使用存储和代理时遇到了一些麻烦)。 但你不能使用 this.load 因为你需要加载模型。也许您需要将 settingsObject 从 localStorage“转换”到模型并加载这个模型!?
哪种方法更好,或者是否有另一种更好的方法来保存/加载设置并以表单显示它们?
How you should save/load settings in an app when you want to use the localStorage?
Let's say we just want to save a username and a password.
Currently I know two options:
- using a model with store and proxy.
This seems to be a bit too "big" as you only want to store one object. But you have the advantage to easily use this.load() in the settings FormPanel. - using the Phonegap or the 'plain' localStorage.
Then you have to use JSON.stringify and JSON.parse but you store only one object and its easier to load it (after a page refresh I run into some trouble using the store and proxy).
But you can't use this.load as you need to load a model. Maybe you need to "cast" the settingsObject from the localStorage to a model and load this one!?
Which approach is better or is there even another, better approach to save/load settings and display them in a form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我绝对同意你的观点,使用 Sencha Touch 模型和存储来存储某些设置是多余的。我建议使用 Ext.util.JSON.encode/decode 序列化您的设置对象。
注意:您将无法序列化对象的函数成员,更不用说属于其原型链上的对象的属性,因此您将无法序列化和反序列化 Ext 模型并使其在您工作时工作。把它拿回来。您能做的最好的事情就是序列化 Ext 组件的配置,并在获取配置后通过其构造函数重新构建它。
对于大多数简单的设置对象,只需将其序列化并将其存储在本地存储中的键中即可。当您到达页面时,取出设置对象并更新您的表单。当您更改表单中的设置时,请将其写入对象。当您离开页面时(onbeforeunload),将对象写入localstore。
I definitely agree with you that using a Sencha Touch model and store is overkill to store some settings. I recommend serializing your settings object with Ext.util.JSON.encode/decode.
Note: You won't be able to serialize the function members of an object though, much less the properties belonging to objects up its prototype chain, so you won't be able to serialize and deserialize an Ext model and have it work when you get it back. The best you could do is serialize the configuration for your Ext component and reconstitute it through its constructor after getting the configuration back.
For most simple settings objects, just serialize it and store it in a key in the local store. When you get to your page, get the settings object out and update your form. When you change a setting in the form, write it to the object. When you leave the page (onbeforeunload), write the object to localstore.