为什么我被迫使用 Ext.getCmp('id') 而不是 this.objectName?

发布于 2025-01-08 05:34:46 字数 2704 浏览 0 评论 0原文

当我使用 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);   
    }
});    

When I reference a combobox using this.stateComboBox it fails. However, using the same syntax, it works fine for a text field.

Giving the combobox an 'id', I can reference it using Ext.getCmp('stateComboBox'). But, I know that is BAD practice.

Can someone please tell me what I am doing wrong? See notes at end.

Thanks

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-01-15 05:34:46

您应该使用 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
}

You should use itemId instead of id. Get the object by calling:

this.getComponent('internalid');

To solve the issue I suspect that you're only creating the reference to early. You need to be careful what you do so you don't end up adding the object to the prototype rather than the inherited object.

Add all your things in initComponent rather than constructor.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文