看起来如果没有“黑客”,他就无法完成。Ext.Msg 是一个单例,在 initComponent 中设置了文本字段并且它是不可配置的。 http://docs.sencha.com/ext-js/4-1/source/MessageBox.html#Ext-window-MessageBox-method-initComponent
由于它是单例,因此覆盖将不起作用,也不是解决问题的好方法。
Messagebox 的扩展应该可以工作,但每次 Ext 升级时都必须查看代码,因为 MessageBox 代码没有很多钩子。
Ext.define('NumberPrompt', {
extend: 'Ext.window.MessageBox',
initComponent: function() {
this.callParent();
var index = this.promptContainer.items.indexOf(this.textField);
this.promptContainer.remove(this.textField);
this.textField = this._createNumberField();
this.promptContainer.insert(index, this.textField);
},
_createNumberField: function() {
//copy paste what is being done in the initComonent to create the textfield
return new Ext.form.field.Number({
id: this.id + '-textfield',
anchor: '100%',
enableKeyEvents: true,
listeners: {
keydown: this.onPromptKey,
scope: this
}
});
}
});
var msgbox = new NumberPrompt().prompt('Quantity', 'Enter a number',function(btn, text){} )