如何在Extjs中获取类实例?

时间:2013-04-28 02:55:14

标签: javascript extjs

我定义了一个类'IMS.SellMgt.testForm'。当我点击'submit'按钮时,我想得到'var test = this.formPanel;',为什么test为null?如果我想得到这个.formPanel,我该怎么办?谢谢!

    Ext.define('IMS.SellMgt.testForm', {
    formPanel: null,
    LoadForm: function () {
            this.formPanel = new Ext.form.Panel({
            renderTo: 'form-ct',
            frame: true,
            title: 'XML Form',
            width: 300,
            items: [{
                xtype: 'fieldset',
                title: 'Contact Information',
                defaultType: 'textfield',
                items: [{
                    fieldLabel: 'First Name',
                    emptyText: 'First Name',
                    name: 'first'
                }
            ]
            }],
            buttons: [{
                text: 'Submit',
                handler: function () {
                    var test = this.formPanel;
                        }
            }]
        });
    }
});

Ext.onReady(function () {
    var frmTest = Ext.create('IMS.SellMgt.testForm', {});
    frmTest.LoadForm();
});

2 个答案:

答案 0 :(得分:0)

尝试类似:

......
handler: function () {
    var test = this.findParentByType(Ext.form.FormPanel);
}
....

答案 1 :(得分:0)

handler是一种为按钮指定click事件侦听器的简单方法。由于它是短手的,它有一些缺点。

使用handler,函数的上下文始终是按钮。换句话说,处理函数内的this是按钮,而不是您的表单。

为了使this成为您的类,请使用指定范围的侦听器:

buttons: [{
    text: 'Submit',
    listeners: {
        click: function() {
            this.formPanel; // will work now
            ...
        },
        scope: this // this is the key
    }
}]

您还可以使用up查找表单:

handler: function() {
    var form = this.up('form');
    ...
}
相关问题