简体   繁体   English

如何在加载组件之前设置隐藏的 window 属性?

[英]How to set hidden window property before the component is loaded?

In my application, I have a log in window with two textfields (email and password) and submit button that submits those values to the server:在我的应用程序中,我使用两个文本字段(电子邮件和密码)登录 window,并提交将这些值提交到服务器的按钮:

    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'
        });
    },

I would like have an option 'remember me on this device' in login window, so the user doesn't need to input credentials every time.我想在登录 window 中有一个选项“在此设备上记住我”,因此用户不需要每次都输入凭据。 My idea was to use cookies to store values for email and password.我的想法是使用 cookies 来存储 email 和密码的值。 So, I added a checkbox in login form, and upon successful log in, if checkbox is checked, I save email in cookie:所以,我在登录表单中添加了一个复选框,成功登录后,如果选中了复选框,我将 email 保存在 cookie 中:

Ext.util.Cookies.set('MyApplication', user_email);

And the same I do for the password.我对密码也是如此。 Those cookies can be cleared on logout.那些 cookies 可以在注销时清除。 At the very beginning of the application I can read those cookies在应用程序的最开始,我可以阅读那些 cookies

Ext.util.Cookies.get('MyApplication');

That could be done at the very beginning or for example on 'beforerenderer' event of login window.这可以在一开始就完成,或者例如在登录 window 的“beforerenderer”事件中完成。
But now comes the part when I should skip appearance of login window if the user selected that option.但是现在如果用户选择了该选项,我应该跳过登录 window 出现的部分。 Actually, I don't want to skip that window at all – I would just like to hide it and to maintain its actions.实际上,我根本不想跳过那个 window - 我只想隐藏它并保持它的动作。

So, is it possible at some place to add something like那么,是否可以在某个地方添加类似

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
}

You can set hidden in beforerender event and submit the form, in this case user will not see it (it will be rendered in hidden mode):您可以在 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()
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM