authentication - 如何在组件加载前设置隐藏窗口属性?
问题描述
在我的应用程序中,我有一个登录窗口,其中包含两个文本字段(电子邮件和密码)和提交按钮,用于将这些值提交到服务器:
doLogin: function() {
var me = this,
form = me.lookupReference('form');
me.getView().mask('Authenticating... Please wait...');
form.submit({
clientValidation: true,
url: 'login.php',
scope: me,
success: 'onLoginSuccess',
failure: 'onLoginFailure'
});
},
我希望在登录窗口中有一个“在此设备上记住我”选项,因此用户不需要每次都输入凭据。我的想法是使用 cookie 来存储电子邮件和密码的值。因此,我在登录表单中添加了一个复选框,成功登录后,如果选中了复选框,我将电子邮件保存在 cookie 中:
Ext.util.Cookies.set('MyApplication', user_email);
我对密码也是如此。这些 cookie 可以在注销时清除。在应用程序的最开始,我可以读取那些 cookie
Ext.util.Cookies.get('MyApplication');
这可以在一开始就完成,或者例如在登录窗口的“beforerenderer”事件上完成。
但是现在如果用户选择了该选项,我应该跳过登录窗口的出现。实际上,我根本不想跳过那个窗口——我只想隐藏它并保持它的动作。
那么,是否可以在某个地方添加类似
if (Ext.util.Cookies.get('MyApplication')){
// 1. hide the login window so it won't appear at all
// 2. Set value of email and password textfields to stored values
// 3. Submit such hidden form to the server
}
解决方案
您可以在 beforerender 事件中设置 hidden 并提交表单,在这种情况下用户将看不到它(它将以隐藏模式呈现):
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.form.Panel', {
title: 'Simple Form',
bodyPadding: 5,
width: 350,
// The form will submit an AJAX request to this URL when submitted
url: 'save-form.php',
// Fields will be arranged vertically, stretched to full width
layout: 'anchor',
defaults: {
anchor: '100%'
},
// The fields
defaultType: 'textfield',
items: [{
fieldLabel: 'Email',
name: 'email',
value: Ext.util.Cookies.get('email'),
allowBlank: false
}, {
fieldLabel: 'password',
name: 'password',
allowBlank: false,
inputType: 'password',
value: Ext.util.Cookies.get('password')
}],
// Reset and Submit buttons
buttons: [{
text: 'Reset',
handler: function () {
this.up('form').getForm().reset();
}
}, {
text: 'Submit',
formBind: true, //only enabled once the form is valid
disabled: true,
handler: function () {
var form = this.up('form').getForm();
if (form.isValid()) {
form.submit({
success: function (form, action) {
Ext.Msg.alert('Success', action.result.msg);
},
failure: function (form, action) {
Ext.Msg.alert('Failed', action.result.msg);
}
});
}
}
}],
listeners: {
beforerender: function(loginWindow) {
if(Ext.util.Cookies.get('email') && Ext.util.Cookies.get('password')) {
console.log('Hidden');
loginWindow.setHidden(true);
loginWindow.getForm().submit();
}
}
},
renderTo: Ext.getBody()
});
}
});
推荐阅读
- xml - 使用 XML Mind Dita Converter 转换为 pdf 时的内容超出页脚
- python - 比较python中的两个数据框列
- python - 如何合并包含相同和不同行和列名称的不同数据框
- postgresql - 在 Postgraphile 中,对多对多关系使用权限
- html - 即使没有添加不透明度,画布和边框也显示透明
- java - 跟踪 Spark 结构化流中的消费消息
- css - Angular:使用 fxflex 将垫子工具提示与左对齐
- django - 通过文件名获取 ImageField 中文件的实例
- html - background-size: 100% 100% 上传后不起作用
- python - 使用 CV2 对视频进行镜像/对称效果