PDA

View Full Version : Overriding button texts



Spenna
9 Nov 2011, 2:13 AM
Hi all.

How can i override the default button texts defined in Ext.window.MessageBox?
I'm trying to override it using the Ext.override method, but seems its not picked up:


Ext.override(Ext.window.MessageBox, {
buttonText: {
ok: 'OK',
yes: 'Ja',
no: 'Nei',
cancel: 'Avbryt'
}
});


I've also tried the example from the docs to no avail (getting message 'Ext.window.MessageBox.buttonText is undefined'):


Ext.window.MessageBox.buttonText.yes = "oui"; //french

mitchellsimoens
9 Nov 2011, 5:03 AM
Ext.window.MessageBox extends Ext.window.Window has has to be instantiated just like creating a Window instance. Before, MessageBox was just a singleton so Ext.MessageBox is a new instance of Ext.window.MessageBox so you can do this in 4.1:


Ext.MessageBox.buttonText = {
ok : 'OK',
cancel : 'Cancel',
yes : 'Yes',
no : 'No'
};

And change them to wherever. My test did this:


Ext.MessageBox.buttonText.ok = 'I Changed!';
Ext.Msg.alert('Test', 'Hello');

And the button that used to say 'OK' now indeed says 'I Changed'.

In 4.0.7:


var ok = Ext.MessageBox.down('#ok');
ok.setText('I Changed!');

Ext.Msg.alert('Test', 'Hello');

And now the 'OK' button now says 'I Changed'. What is happening is when a new instance of Ext.window.MessageBox is created, it automatically creates an Array of buttons so to change the text, you need to get that button instance and change it's text. Each button has an itemId:

OK button itemId: 'ok'
Yes button itemId: 'yes'
No button itemId: 'no'
Cancel button itemId: 'cancel'

If you want a cross version solution, you could create you own MessageBox creating an instance of Ext.window.MessageBox and passing in the text as config:


Ext.MessageBox = Ext.Msg = Ext.create('Ext.window.MessageBox', {
buttonText: {
ok : 'OK',
yes : 'Yes',
no : 'No',
cancel : 'Cancel'
}
});

All of this code has been tested on 4.1 or 4.0.7 and works 100%

Spenna
16 Nov 2011, 2:27 AM
Thanks a bunch, it worked.