Hi,
this question cannot be answered in general. Usually I look at my component,
and then the Ext component I extend, and decide based on that.
If my component just needs to unregister listeners on foreign observables, it
is fine to use the "destroy" event (if available), or override the onDestroy
method (if available).
Now when to use beforedestroy and when to use destroy?
destroy handlers will only be called if the component is actually destroyed,
i.e. if none of the beforedestroy listeners has returned false. If we destroy
in beforedestroy, our components will be destroyed even if an application level
listener returns false and stops the destroy sequence. But the caveat here is:
"destroy" is called after the component is destroyed, so we cannot interact
with the destroyed component anymore. The beforeDestroy method might be a good
choice, because the component is still available, but it will only be called if
none of the beforedestroy listeners returned false.
Also, it is not necessary to unregister listeners on the component that we are
listening for. Looking at the handler for the target's beforedestroy event in
the PrintPageField, I see that it would have been better to register for the
target's destroy event (instead of beforedestroy), and change the code from
onBeforeDestroy: function() {
this.target.un("beforedestroy", this.onBeforeDestroy, this);
this.target.un("select", this.onFieldChange, this);
this.target.un("valid", this.onFieldChange, this);
this.printPage.un("change", this.onPageChange, this);
this.printPage.printProvider.un("layoutchange", this.onLayoutChange,
this);
}
to
onDestroy: function() {
this.printPage.un("change", this.onPageChange, this);
this.printPage.printProvider.un("layoutchange", this.onLayoutChange,
this);
}
I hope this helps.
Andreas.
On Nov 5, 2010, at 21:18 , Eric Lemoine wrote:
> On Friday, November 5, 2010, Bart van den Eijnden <[email protected]> wrote:
>> Hi list,
>>
>> this discussion came up in ticket 369.
>>
>> Currently we use the private functions beforeDestroy and onDestroy quite a
>> bit in GeoExt.
>>
>> Shouldn't we be using the public events beforedestroy and destroy instead?
>
> IIRC we decided against relying on events inside our components in the
> early stages of GeoExt development. I can't remember the exact
> reasons, but I think it made sense to build components just as Ext
> does, using onRender, afterRender, etc. Also, we want to avoid
> situations where code of our components aren't executed because some
> application code has stopped the callback execution chain (by
> returning false).
>
> Now to answer fredj's question - onDestroy vs afterDestroy - I'd say
> it depends. It depends whether you want to be called "on" destroy or
> "after" destroy :-) I'd say use onDestroy when you can, afterDestroy
> when onDestroy is too early for what you need to do.
>
> Just my 2 cents,
>
> --
> Eric Lemoine
>
> Camptocamp France SAS
> Savoie Technolac, BP 352
> 73377 Le Bourget du Lac, Cedex
>
> Tel : 00 33 4 79 44 44 96
> Mail : [email protected]
> http://www.camptocamp.com
> _______________________________________________
> Dev mailing list
> [email protected]
> http://www.geoext.org/cgi-bin/mailman/listinfo/dev
--
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.
_______________________________________________
Dev mailing list
[email protected]
http://www.geoext.org/cgi-bin/mailman/listinfo/dev