Is publishing / subscribing to events after the user interface provides best practice regardless of structure?

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

+6
source share
1 answer

You must balance the overhead of event processing with the ability to skip significant events. In js / DOM, the state of the earth is volatile.

For your # 1, if you can determine the point in time when all your subcomponents were displayed and signed, triggering the init event makes sense.

For # 2, it seems everyone can listen to events; however, this may slow down the work. If performance issues are obvious, you may have to decide what events you don't need and not subscribe.

0
source

Source: https://habr.com/ru/post/901667/


All Articles