javascript Sencha Touch 2 - 如何获取表单值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9684808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:26:43  来源:igfitidea点击:

Sencha Touch 2 - How to get form values?

javascriptsencha-touchextjssencha-touch-2

提问by Roman

I've got a Sencha Touch 2 MVC app with a form as a view. I'm trying to get it's values from the controller with no success.

我有一个带有表单作为视图的 Sencha Touch 2 MVC 应用程序。我试图从控制器获取它的值但没有成功。

How could this be done? I'm posting my view/controller code for this one.

这怎么可能?我正在发布我的视图/控制器代码。

View:

看法:

Ext.define('MyApp.view.LoginForm', {
    extend: 'Ext.form.Panel',
    config: {
        fullscreen: true,
        items: [
            {
                xtype: 'fieldset',
                title: 'Login',
                id: 'loginform',
                items: [
                    {
                        xtype: 'emailfield',
                        name: 'email',
                        label: 'Email'
                    },
                    {
                        xtype: 'passwordfield',
                        name: 'password',
                        label: 'Password'
                    }
                ]
            },
            {
                xtype: 'button',
                width: '50%',
                text: 'Login',
                ui: 'confirm',
                id: 'btnSubmitLogin'
            },
            {
                xtype: 'toolbar',
                docked: 'top',
                title: 'MyApp Mobile'
            }
        ]
    }
});

And the controller:

和控制器:

Ext.define("MyApp.controller.LoginForm", {
    extend: "Ext.app.Controller",
    config: {
        refs: {
            btnSubmitLogin: "#btnSubmitLogin"
        },
        control: {
            btnSubmitLogin: {
                tap: "onSubmitLogin"
            }
        }
    },
    onSubmitLogin: function () {
        console.log("onSubmitLogin");
        var values = app.views.LoginForm.loginform.getValues();
        TryLogin(values['email'], values['password']);
    },
    launch: function () {
        this.callParent();
        console.log("LoginForm launch");
    },
    init: function () {
        this.callParent();
        console.log("LoginForm init");
    }
});

The code will go up to

代码将上升到

console.log("onSubmitLogin");

And then stop.

然后停下来。

On launch I use this:

在启动时我使用这个:

var LoginForm = Ext.create("MyApp.view.LoginForm");
Ext.Viewport.add(LoginForm);

So, how can I get the values?

那么,我怎样才能得到这些值呢?

Thanks

谢谢

回答by Roman

Ok. Found the answer after some tweaking. For the sake of future generations - here is the solution:

行。经过一些调整后找到了答案。为了子孙后代 - 这是解决方案:

I've added id:'loginform'to the LoginForm and then, in the controller in the 'refs' part I've added loginForm: '#loginform'. Then I could use it as :

我已经添加id:'loginform'到 LoginForm 中,然后在控制器的“refs”部分中添加了loginForm: '#loginform'. 然后我可以将它用作:

var values = this.getLoginForm().getValues();

Good luck to all

祝你们好运

回答by wdragon

Try this

试试这个

Controller:

控制器:

    Ext.define("mySIMS.controller.LoginForm", {
        extend : "Ext.app.Controller",
        config : {
            refs : {
                btnSubmitLogin : "#btnSubmitLogin",
                LoginForm : '#loginform'
            },
            control : {
                btnSubmitLogin : {
                    tap : "onSubmitLogin"
                }
            }
        },
        onSubmitLogin : function() {
            console.log("onSubmitLogin");
            var values = this.getLoginForm().getValues();
            console.log(values);

            //var values = Oreilly.views.LoginForm.loginform.getValues();

        },
        launch : function() {
            this.callParent();
            console.log("LoginForm launch");
        },
        init : function() {
            this.callParent();
            console.log("LoginForm init");
        }
    });

View:

看法:

    Ext.define('mySIMS.view.LoginForm', {
        extend : 'Ext.form.Panel',
        config : {
            id : 'loginform',
            fullscreen : true,
            items : [{
                xtype : 'fieldset',
                title : 'Login',
                items : [{
                    xtype : 'emailfield',
                    name : 'email',
                    label : 'Email'
                }, {
                    xtype : 'passwordfield',
                    name : 'password',
                    label : 'Password'
                }]
            }, {
                xtype : 'button',
                width : '8%',
                text : 'Login',
                ui : 'confirm',
                id : 'btnSubmitLogin'
            }, {
                xtype : 'toolbar',
                docked : 'top',
                title : 'Mobile'
            }]
        }
    });

Hope this help.

希望这有帮助。