【问题标题】:'this' not resolved correctly in submit handler in Sencha touch/JavaScript'this' 在 Sencha touch/JavaScript 的提交处理程序中未正确解决
【发布时间】:2012-02-11 13:54:48
【问题描述】:

我正在尝试通过 sencha touch 创建一个表单,该表单只需获取用户输入的文本并在网站上搜索工作。虽然其他一切都正常工作,包括重置按钮,但每当我在提交按钮处理程序中执行 this.getComponent('keywords') 时,它都会给我一个错误,说 Uncaught

TypeError: Object # 没有方法“getComponent”。 Ext.ns('SearchJobsForm'); // 注册我们的命名空间

var resetButtonHandler = function (btn, evt) {
this.getComponent('keywords').reset();
this.getComponent('dposted').reset();
this.getComponent('jtitle').reset();
this.getComponent('jcategory').reset();
this.getComponent('salaryf').reset();
this.getComponent('salaryt').reset();
this.getComponent('jscategory').reset();
this.getComponent('ptype').reset();
}

这是问题所在的代码块。以下代码块没有像上面的重置按钮处理程序那样解决“this”。

        var submitButtonHandler = function(btn,evt) {
        var temp = this.getComponent('keywords').getValue();
        //query('#jcategory').getValue();
         alert(temp);
        //alert('In Progress');
        }

以下只是创建表单。

        SearchJobsForm.form = Ext.extend(Ext.Panel,{

            initComponent: function(){

                Ext.apply(this, {
                    floating: true,
                    width: 250,
                    height: 370,
                    scroll: 'vertical',
                    centered: true,
                    modal: true,
                    hideOnMaskTap: false,
                    items: [{  
                    xtype: 'textfield',
                    itemId: 'keywords',
                    label: 'Keywords',
                    labelAlign: 'top',
                    labelWidth: '100%',
                    name: 'Keywords'
                    },{  
                    xtype: 'textfield',
                    label: 'Job Title',
                    itemId: 'jtitle',
                    labelAlign: 'top',
                    labelWidth: '100%',
                    name: 'Job Title'
                    },{
                    xtype: 'selectfield',
                    label: 'Job Category',
                     itemId: 'jcategory',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '-- ANY --', value: 'ANY'
                        }, {
                            text: 'Technical', value: 'Technical'
                        }, {
                            text: 'Non-Technical', value: 'Non-Technical'
                        }, {
                            text: 'Tech Start-up', value: 'Tech Start-up'
                        }, {
                            text: 'Life Science', value: 'Life Science'
                        }, {
                            text: 'Digital Media', value: 'Digital Media'
                        }, {
                            text: 'Accelerator Centre', value: 'Accelerator Centre'
                        }
                        ]
                    },{
                    xtype: 'selectfield',
                    label: 'Job Sub-Category',
                     itemId: 'jscategory',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '-- ANY --', value: 'ANY'
                        }, {
                            text: 'Developer', value: 'Developer'
                        }, {
                            text: 'Quality Assurance', value: 'Quality Assurance'
                        }, {
                            text: 'Project Manager', value: 'Project Manager'
                        }, {
                            text: 'Tester', value: 'Tester'
                        }, {
                            text: 'IT Help Desk', value: 'IT Help Desk'
                        }, {
                            text: 'Health Care', value: 'Health Care'
                        }, {
                            text: 'Transportation and Logistics', value: 'Transportation and Logistics'
                        }, {
                            text: 'Management', value: 'Management'
                        }, {
                            text: 'Network', value: 'Network'
                        }, {
                            text: 'Administration', value: 'Administration'
                        }, {
                            text: 'General', value: 'General'
                        }
                        ]
                    },{
                    xtype: 'selectfield',
                    label: 'Position Type',
                     itemId: 'ptype',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '-- ANY --', value: 'ANY'
                        }, {
                            text: 'Part Time', value: 'Part Time'
                        }, {
                            text: 'Part Time Contract', value: 'Part Time Contract'
                        }, {
                            text: 'Full Time', value: 'Full Time'
                        }, {
                            text: 'Full Time Contract', value: 'Full Time Contract'
                        }
                        ]
                    },{
                    xtype: 'selectfield',
                    label: 'Salary (CAD$): From',
                     itemId: 'salaryf',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '-- ANY --', value: 'ANY'
                        }, {
                            text: '20000', value: '20000'
                        }, {
                            text: '30000', value: '30000'
                        }, {
                            text: '40000', value: '40000'
                        },{
                            text: '50000', value: '50000'
                        }, {
                            text: '60000', value: '60000'
                        }, {
                            text: '70000', value: '70000'
                        }, {
                            text: '80000', value: '80000'
                        }, {
                            text: '90000', value: '90000'
                        }, {
                            text: '100000', value: '100000'
                        }, {
                            text: '100000+', value: '100000+'
                        }

                        ]
                    },{
                    xtype: 'selectfield',
                    label: 'to',
                     itemId: 'salaryt',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '-- ANY --', value: 'ANY'
                        }, {
                            text: '20000', value: '20000'
                        }, {
                            text: '30000', value: '30000'
                        }, {
                            text: '40000', value: '40000'
                        },{
                            text: '50000', value: '50000'
                        }, {
                            text: '60000', value: '60000'
                        }, {
                            text: '70000', value: '70000'
                        }, {
                            text: '80000', value: '80000'
                        }, {
                            text: '90000', value: '90000'
                        }, {
                            text: '100000', value: '100000'
                        }, {
                            text: '100000+', value: '100000+'
                        }
                        ]
                    },{
                    xtype: 'selectfield',
                    label: 'Posted in last (Days):',
                     itemId: 'dposted',
                    labelAlign: 'top',
                    labelWidth: '100%',
                        options: [{
                            text: '30', value: '30'
                        }, {
                            text: '60', value: '60'
                        }, {
                            text: '90', value: '90'
                        }
                        ]
                    }
                        ],
                        dockedItems: [{
                                        xtype: 'toolbar',
                                        itemId: 'toolbar',
                                        dock: 'bottom',
                                        height: '36',
                                        items: [
                                                { xtype: 'button', text: 'Reset',itemId: 'reset',scope: this,
                                                handler:  resetButtonHandler  },
                                                { xtype: 'spacer'},
                                                { xtype: 'button', text: 'Submit',
                                                handler: submitButtonHandler 
                                                }
                                                ]
                                    }]


                });


                SearchJobsForm.form.superclass.initComponent.call(this);

               // alert(SearchJobsForm.form.getValues(true));

                this.items.get(2).on({
                    change: this.onChange,
                    scope: this
                });



            },


            onChange: function(selectField, value){
                this.items.get(1).disable();
            } //end of function onChange

        });



        Ext.setup({
            tabletStartupScreen: 'tablet_startup.png',
            phoneStartupScreen: 'phone_startup.png',
            icon: 'icon.png',
            glossOnIcon: false,
            onReady: function(){

                var form = new SearchJobsForm.form();
                form.show();                

            }
        });

【问题讨论】:

    标签: javascript sencha-touch


    【解决方案1】:

    this.getComponent(childId) 浏览当前对象的子组件。在 resetButtonHandler() 中,this 应该被视为重置按钮。

    SearchJobsForm 是一个对象的声明而不是它的实例。否则,您将使用“新”命令。 SearchJobsForm.fieldName 不引用字段。

    因此,在 submitButtonHandler() 方法的上下文中,this 等价于:Ext.getCmp('reset');

    您可以使用组件的 id 字段(不是 itemId)以快速而肮脏的方式解决它:

    SearchJobsForm.form = Ext.extend(Ext.Panel,{

    initComponent: function(){
      Ext.apply(this, {
        id: 'form', // add an id to use Ext.getCmp()            
        floating: true,
        ...
      }
    });
    
    // So later you can use 
    Ext.getCmp('keywords') 
    

    或者一路up从resetButton上下文到表单面板,然后down到输入关键字。在这种情况下无需添加属性。

    var form = this.el.up('.x-panel');
    var input_keyword = form.down('.x-input-text[name=Keywords]').getValue();
    

    问候

    【讨论】:

    • 我刚刚完成了这段代码的测试。实际上,up() 和 down() 使用 CSS 选择器。而且我弄错了 itemId 和 id 属性。我的记忆还太年轻,不会失败。它现在正在工作。
    • 嘿沃姆。我对 Sencha touch 中的 ListView 有另一个问题。你能简单看看我的另一篇帖子吗:stackoverflow.com/questions/8912160/…
    • 嗯,我今晚去看看
    • Vaugham,我编辑了一堆,更多编辑的代码在:stackoverflow.com/questions/8929322/… 但是如果你喜欢,你可以看看两者。然而,更新/工作的代码是后者!
    • 关于如何拥有多个屏幕的任何建议?它应该做的是转到一个新屏幕,但它不会改变屏幕上的任何内容,并将我的列表视图放在浏览器的可视区域之外。 CLICK FOR CODE有代码
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2011-12-07
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多