如何在本地存储中存储登录凭据 [英] How to store login credentials in localstorage
问题描述
如果有sencha touch应用程序,并带有登录表单.对于回头用户,我想将登录凭据存储在本地存储中.
If got a sencha touch application, with a login form. For returning users, I would like to store the login credentials in the local storage.
User.js
App.models.User = Ext.regModel('User', {
fields: [
{
name: 'username',
type: 'string'
}, {
name: 'password',
type: 'string'
}
],
proxy: {
type: 'localstorage',
id: 'p2s-users'
}
});
Users.js
App.stores.users = new Ext.data.Store({
model: 'User',
autoLoad: true
});
现在我尝试将表单值与本地存储同步,我该怎么做,读取/获取表单的活动流程是什么?
Now I try to sync the form values with local storage, how can I do that, what's the activity flow for reading / getting it?
为了阅读,我会这样做:
For reading I would do:
var model = new App.models.User();
users = App.stores.users.load();
if (users.data.length > 0 ) {
model = users.getAt(0);
} else {
App.stores.users.create();
}
form.load(model);
写作:
form.model.set(form.data);
form.model.save();
App.stores.users.sync();
以某种方式,它看起来对我来说太复杂了(无论如何它还是行不通的).您将如何解决该问题?
Somehow it looks too complicate to me (and it doesn't work anyway). How would you solve the problem?
推荐答案
首先请确保您正确保存了模型.例如,执行以下操作:
First make sure your saving the models right. For example do this:
var model = new App.models.User();
model.data.username = 'theUser';
model.data.password = 'thePass';
model.save(); //the actual saving. Use chrome developer tools to see the local storage
然后从本地存储加载模型
Then load the model from the localstorage
users = App.stores.users.load();
model = users.getAt(0);
检查加载的模型是否正常,然后使用load(model)
Check if the loaded model is OK and load that to the form with load(model)
表单可能是这样的:
new Ext.Application({
launch: function() {
new Ext.form.FormPanel({
id:'theForm',
fullscreen: true,
defaults: {
labelAlign: 'top',
labelWidth: 'auto',
},
items: [
{
id : 'userName',
name : 'username',
label: 'Email',
xtype: 'emailfield',
},
{
id : 'userPassword',
name: 'password',
label: 'Password',
xtype: 'passwordfield',
}
]
});
}
});
然后像这样Ext.getCmp('theForm').load(users.getAt(0));
要从以下形式获取模型:Ext.getCmp('theForm').getRecord();
To get the model from the form: Ext.getCmp('theForm').getRecord();
这篇关于如何在本地存储中存储登录凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!