2013-04-28 98 views
0

我定义了一个类'IMS.SellMgt.testForm'。当我点击'submit'按钮时,我想要'var test = this.formPanel;',为什么测试是空的?如果我想得到this.formPanel,我该怎么办?谢谢!如何在Extjs中获得类实例?

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(); 
}); 

回答

0

尝试类似:

...... 
handler: function() { 
    var test = this.findParentByType(Ext.form.FormPanel); 
} 
.... 
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'); 
    ... 
}