2013-04-28 49 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'); 
    ... 
}