PDA

View Full Version : AbstractComponent - can I override the onDestroy method?



LesJ
16 Feb 2012, 9:21 AM
Can a subclass override the AbstractComponent.onDestroy method? ... or is this method private and should I override the beforeDestroy method instead?


Ext.define('Ext.AbstractComponent', {
...
/**
* Invoked before the Component is destroyed.
*
* @method
* @template
* @protected
*/
beforeDestroy : Ext.emptyFn,
...
onDestroy : function() {
...
},
....
});

mitchellsimoens
16 Feb 2012, 9:53 AM
Any subclass can override it but it does a few things that you would need to clean up. I would just do a callParent

skirtle
16 Feb 2012, 3:57 PM
Personally I wouldn't hesitate to override onDestroy if I needed to add destruction logic to a custom component. Choosing between onDestroy and beforeDestroy seems to be just a matter of timing.

Methods with names that sound like events (e.g. onDestroy, afterRender) are to be thought of as 'internal events', intended to be overridden in subclasses. Of course you have to take care to call the overridden method at the appropriate time.

There are some really old posts that you may find interesting. Here's one from Jack Slocum explaining his use of template methods in ExtJS 1. This principle remains in the current framework:

http://www.sencha.com/forum/showthread.php?6481&p=33312&viewfull=1#post33312

Later in the same thread he touches upon why he chose to name some methods after events:

http://www.sencha.com/forum/showthread.php?6481&p=33652&viewfull=1#post33652

This blog post from Nige White also touches upon this subject. Most of the post is about writing plugins but if you skim down you'll find he discusses some of the template methods in the framework:

http://www.sencha.com/blog/advanced-plugin-development-with-ext-js/

He also explains some of the reasons why overriding methods is more appropriate than listening to events when writing an extension.