为什么我被迫使用 Ext.getCmp('id') 而不是 this.objectName?
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 itemId 而不是 id。通过调用获取对象:
为了解决这个问题,我怀疑您只是创建了对早期的引用。您需要小心所做的事情,以免最终将对象添加到原型而不是继承的对象中。
将所有内容添加到 initComponent 而不是构造函数中。
You should use itemId instead of id. Get the object by calling:
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.