2012-02-21 37 views
0

當我使用this.stateComboBox引用組合框時,它失敗。但是,使用相同的語法,對於文本字段而言,它工作正常。爲什麼我被迫使用Ext.getCmp('id')而不是this.objectName?

給組合框'id',我可以使用Ext.getCmp('stateComboBox')來引用它。但是,我知道這是不好的做法。

有人能告訴我我做錯了什麼嗎?在最後看到筆記。

感謝

Ext.define('App.view.prospects.Show', { 

    alias:      'widget.prospectsshow', 
    extend:      'Ext.form.Panel', 
    iconCls:      'icon-prospects', 
    itemId:      'prospectsshow', 

    constructor:     function(config) { 

     var cfg = config || {}; 

     this.phoneNumberTextField = Ext.create('Ext.form.field.Text', { 
      anchor:     '100%', 
      allowBlank:    true, 
      fieldLabel:    'Phone Number', 
      labelAlign:    'top', 
      margin:     '5 5 5 0', 
      tabIndex:     1 
     }); 

     this.stateComboBox = Ext.create('Ext.form.field.ComboBox', { 
      anchor:     '100%', 
      displayField:    'name', 
      editable:     false, 
      fieldLabel:    'State', 
      forceSelection:   true, 
      id:      'stateComboBox', // I hate using this. See note below. 
      labelAlign:    'top', 
      margin:     '5 5 5 5', 
      mode:      'local', 
      store:      this.stateStore, 
      tabIndex:     22, 
      triggerAction:   'all', 
      valueField:    'id', 
      valueNotFoundText:  '' 
     }); 

     // Lots of objects removed for clarity.... 

     Ext.applyIf(cfg, { 
      border:     false, 
      items:      Ext.create('Ext.form.Panel', { 
       bodyStyle:   'background-color: #F1F1F1;', 
       items:     this.prospectPanel // Not shown above, but contains this.phoneNumberTextField and this.stateComboBox 
      }), 
      frame:      false, 
      layout:     'fit' 
     }); 

     this.superclass.constructor.call(this, cfg); 
    }, 

    setData:       function(record) { 

     // This works fine. 
     this.phoneNumberTextField.setValue(record.phone_number); 

     // This fails. No error in console. Just does nothing. WHY? 
     //this.stateComboBox.setValue(record.state_id); 

     // This works. But, I hate using 'id'. It is BAD practice. 
     Ext.getCmp('stateComboBox').setValue(record.state_id); 
    } 
});  

回答

3

您應該使用,而不是ID的itemId。通過電話獲取對象:

this.getComponent('internalid'); 

要解決此問題,我懷疑您只是在創建早期引用。你需要小心你所做的事情,所以你最終不會將對象添加到原型而不是繼承對象。

將所有的東西添加到initComponent而不是構造函數中。

initComponent: function() 
{ 

    // you can add things to config here 
    var config = { 
     anchor: '100% 100%', 
     ... 
    }; 

    // create your local var things here (don't forget to add it somewhere) 
    this.combo = Ext.create('...'); 

    // apply new config to base config 
    Ext.apply(this, config); 

    // ExtJS does all the stuff with the config 
    this.callParent(); 

    // from here you should be able to getComponent 
    // not always true though depending on rendering 
} 
+0

當我這樣做,我收到錯誤信息; 「Uncaught TypeError:無法調用未定義的方法'setValue'。」 如果它應該工作 - 那麼也許這提供了我做錯了什麼的線索。 爲了澄清,我將'id'參數更改爲'itemId'。然後,我將'setValue'行更改爲: this.getComponent('stateComboBox')。setValue(record.state_id); 返回上述錯誤。 – Eric 2012-02-21 15:10:44

+0

此外,建議使用this.getComponent('internalid');並沒有解釋爲什麼對象引用對文本字段使用this.phoneNumberTextField.setValue(record.phone_number);但對於組合框失敗。 – Eric 2012-02-21 16:59:36

+0

嘿謝謝。稍後再試一次。但是,我正在使用initComponent(這是我的標準做法),並因爲我閱讀的另一篇文章而轉而使用構造函數。 [這篇文章](http://stackoverflow.com/questions/6555136/extjs-4-what-is-the-proper-way-to-perform-inheritance)顯示5種方式,我知道2更多我認爲。真正的問題是ExtJS允許使用很多方法,不可能清楚地瞭解每個方法的優缺點,或者爲什麼只有一種方法不能總是有效。 – Eric 2012-02-21 20:43:58

相關問題