PDA

View Full Version : How to add a panel toolbar from a plugin?



nsolsen
14 Oct 2010, 4:57 AM
I am trying to create an Ext.Panel plugin that manipulates the top toolbar. If a toolbar exists, the plugin must add some stuff to it. If no toolbar exists, the plugin must create one and add it to the panel.

I can successfully modify an existing toolbar from the plugin. My problem is how to add one from the plugin init() method. The problem seems to be that all the panel toolbar setup is done during initComponent, and that plugin initialization is done after the initComponent phase completes.

As far as I can see, there is no API to add a toolbar to a panel after initComponent processing is completed. Am I missing something? :-/

Here a simplified example of what I am trying to do:



var myPlugin = {
init: function(parent) {
var tbar;

if(parent instanceof Ext.Panel) {
tbar = parent.getTopToolBar();

if(tbar) {
// Insert some items in the beginning of the existing in toolbar
tbar.insert(0, new Ext.Toolbar.Foo());
tbar.insert(1, new Ext.Toolbar.Bar());
tbar.insert(2, new Ext.Toolbar.Fill());
} else {
//
// Create a new toolbar here -- but HOW?
//
}
}
}
};
Thanks,
Niels

nsolsen
14 Oct 2010, 6:21 AM
I found a way to do this, but unfortunately it means manipulating a private member of the Panel class.



var myPlugin = {
init: function(parent) {
var tbar;

if(parent instanceof Ext.Panel) {
tbar = parent.getTopToolBar();

if(!tbar) {
tbar = new Ext.Toolbar({items: []});

// YUK! Messing with the Panel's private parts.... :(
parent.topToolbar = tbar;
}

tbar.insert(0, new Ext.Toolbar.Foo());
tbar.insert(1, new Ext.Toolbar.Bar());
tbar.insert(2, new Ext.Toolbar.Fill());
}
}
};
It seems to work, but I can't be sure that it works in all cases, and that it will continue to work i later ExtJS releases. :(