Here is another approach:
It avoids the getCmp
call and does not use hard coded IDs which couples your logic to specific IDs which may be error prone if you extend your application. By using hard coded IDs you may run in conflicts with other parts of your application if you assign the same ID twice.
Additional this approach uses the concept of ViewControllers
and references
which is the proposed way by Sencha nowadays to set up your view logic especially for large applications (see ViewControllers).
Ext.define('MyApp.view.foo.FilterController', {
extend: 'Ext.app.ViewController',
alias: 'controller.FilterController',
bt1Event: function () {
alert('bt1 clicked');
},
bt2Event: function () {
alert('bt2 clicked');
// get a reference to the first button
var bt1 = this.lookupReference('bt1');
bt1.fireEvent('click', bt1);
}
});
Ext.define('MyApp.view.foo.Foo', {
extend: 'Ext.panel.Panel',
bodyPadding: 5, // Don't want content to crunch against the borders
width: 300,
title: 'Filters',
controller : 'FilterController',
defaults : {
xtype : 'button'
},
items: [{
text: 'Button 1',
reference: 'bt1',
listeners : {
click: 'bt1Event' // handled by view controller
}
}, {
text: 'Button 2',
reference: 'bt2',
listeners : {
click: 'bt2Event' // handled by view controller
}
}]
});
Ext.create('MyApp.view.foo.Foo', {
renderTo: Ext.getBody()
});
I created a fiddle to demonstrate this approach Sencha Fiddle