Почему я вынужден использовать Ext.getCmp ('id') вместо this.objectName? - PullRequest
0 голосов
/ 21 февраля 2012

Когда я ссылаюсь на комбинированный список, используя this.stateComboBox, он не работает. Однако, используя тот же синтаксис, он отлично работает для текстового поля.

Давая списку со списком '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);   
    }
});    

1 Ответ

3 голосов
/ 21 февраля 2012

Вы должны использовать itemId вместо 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
}
...