2012-02-21 12 views
0

this.stateComboBoxを使ってコンボボックスを参照すると失敗します。ただし、同じ構文を使用すると、テキストフィールドで正常に動作します。this.objectNameの代わりにExt.getCmp( 'id')を使用しなければならないのはなぜですか?

コンボボックスに '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の代わりにIDを使用する必要があります。オブジェクトを呼び出す:

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

+0

また、this.getComponent( 'internalid');を使用するための提案。 this.phoneNumberTextField.setValue(record.phone_number);でテキストフィールドのオブジェクト参照が機能する理由を説明していません。コンボボックスでは失敗します。 – Eric

+0

おかげで。後でもう一度試してみましょう。しかし、私はinitComponentを使用していました(これは私の標準的な習慣です)、私が読んだ別の投稿のためにConstructorを使用しました。 [この記事](http://stackoverflow.com/questions/6555136/extjs-4-what-is-the-properway-to-perform-inheritance)には5つの方法が示されています思う。本当の問題は、ExtJSが非常に多くのメソッドを許していることです。各メソッドの長所や短所を明確に理解することは不可能です。 – Eric

関連する問題