View Full Version : How to Remove a Listener?

16 Sep 2009, 11:33 AM
I've seen lots of references to this, but I can't find any clear example of how this should work. I have a toolbar that I'm populating with a set of buttons performing default actions. The toolbar will be made available to multiple apps that may need to redefine the handler for any given button.

As far as I can tell, the only way to do so (in theory) is to remove the existing listener and add a new listener. I can add a new listener just fine, but I can't find the right voodoo to apply to remove the default listener.

I have this:

var tb = new Ext.Toolbar({
height: 30,
width: 'auto',
items: [
text: 'A Default Button',
id: 'toolbar-button-default',
handler: do_default_thing
After the toolbar is rendered, I have the following code in a later js include file:

btn = tb.findById( 'toolbar-button-default' );
// btn.addListener( 'click', do_custom_thing );
btn.removeListener( 'click', do_default_thing );
Is the function handler not the same even though it's referencing the old function directly? If not, what do I need to do to make it operate as expected? Is there a way to do this with anonymous functions? Ideally, I'd like to set the handler to an anonymous function for clarity, but I thought it would be easier to reference a standalone function for removal.

Much appreciated.

17 Sep 2009, 4:45 PM
The handler, in this case is different from a listener. If you have a peek at the source you'll see that the handler and listeners are fired off one after the other.

Check out the setHandler method instead.