PDA

View Full Version : Helper functions in pre-configured classes



ctp
23 Nov 2010, 7:15 AM
Howdy,

what's the right place for helper functions in a pre-configured class? In my case I try to use my helper function addTab() within the widgets config but I get an 'addTab is not defined'. Tried to define addTab() outside and inside the config.


App.widget.HeaderToolbar = Ext.extend(Ext.Toolbar, {

addTab : function(widget, xtype, title) {
var center = Ext.getCmp('center');
var tab = center.findById(widget);
if (tab) {
center.setActiveTab(tab);
} else {
var tab = center.add({
xtype: xtype,
title: bundle.getMsg(title)
});
center.setActiveTab(tab);
}
},

constructor: function(config) {
var config = Ext.apply(config || {},
{
defaultType: 'button',
items: [{
text: bundle.getMsg('properties'),
menu: [{
text: bundle.getMsg('list_properties'),
handler: addTab('propertyListPanel', 'property_list_panel', 'list_properties')
},{
text: bundle.getMsg('new_property'),
handler: addTab('propertyFormPanel', 'property_form_panel', 'new_property')
},{
text: bundle.getMsg('online_import_export'),
handler: addTab('marketImportExportFormPanel', 'market_import_export_form_panel', 'online_import_export')
}]
}]
});
App.widget.HeaderToolbar.superclass.constructor.call(this, config);
}
});
Ext.reg('header_toolbar', App.widget.HeaderToolbar);

Condor
23 Nov 2010, 7:29 AM
1. addTab is an object method and not local, so you should use 'this.addTab'.
2. Currently, you are assigning the RESULT of the addTab call to the handler. addTab isn't returning anything, so all your handlers are 'undefined'.

You want:

handler: this.addTab.createDelegate(this, ['propertyListPanel', 'property_list_panel', 'list_properties'])

ctp
23 Nov 2010, 8:01 AM
Hi Condor,

thanks for your hint. CreateDelegate does the magic ;-) Just one another question: if I wanna apply e.g. 'closable: true' to all tabs added dynamically by addTab(), where should I define it? Added it to addTab() but without success:


addTab: function(widget, xtype, title) {
var center = Ext.getCmp('center');
var tab = center.findById(widget);
if (tab) {
center.setActiveTab(tab);
} else {
var tab = center.add({
xtype: xtype,
title: bundle.getMsg(title),
closable: true
});
center.setActiveTab(tab);
}
}

Condor
23 Nov 2010, 8:11 AM
That should work!

ps. findById is only going to work if the tabs you add actually have an id!