I have inherited a fairly large Javascript / ExtJS3 database, and there are many instances of event invocations inside the overridden initComponent method after calling "... superclass.initComponent.apply (this, arguments)"., Specific events raise certain objects in a way like:
this.filter.on('filterUpdated', function(filter, params)
I started converting the code to use the pub / sub paradigm instead to reduce the relationship between objects and their specific event names, but quickly ran into problems when publishing and / or subscribing to events in initComponent (which is done in ExtJS before rendering). I need to fire the "INIT" event from the highest-level component when the first screen loads, and I either got an error (due to ExtJS templates "it didn’t work out, as it turned out), or events that don't fire at all.
Then I read the following in the ExtJS source for Ext.Component (from which all components are extensible), and I had a "aha" moment:
if (this.tpl) { if (!this.tpl.compile) { this.tpl = new Ext.XTemplate(this.tpl); } if (this.data) { this.tpl[this.tplWriteMode](contentTarget, this.data); delete this.data; } } this.afterRender(this.container);
When I switched to publishing the "INIT" event from my topmost component afterRender method and subscribing to all events from all other components from their afterRender methods, everything worked as I expected. And now I'm just curious, mainly to test my design ....
Is this a common way to implement pub / sub in an event driven user interface? Regardless of the structure, even? Namely, the following two good principles or their other methods?
- “Initialization events” should be published after all subcomponents are displayed.
- All subcomponents must subscribe to all events (to be safe) after they display
Thanks in advance
source share