Ordering of event handlers of Views inheritance

Hi,
In order to customize and set standards to each and every screen in my project I have developed a View class that extends Jmix’s StandardView, lets call it CustomBaseView in this context.

However, I came to realize that any screen that would inherit my CustomBaseView could not share the same Event Handler as the base screen. Eg. CustomBaseView has a onBeforeShow event handler to perform some actions required, my screen that extends this will not be able to generate onBeforeShow event handler using the IDE’s “Generate Handler” function anymore.

Even if I try to manually type out the structure of the onBeforeShow event handler, it has been tested that it is inconsistent on whether which onBeforeShow will run first. Sometimes the onBeforeShow in the CustomBaseView will run first, sometimes the onBeforeShow on my actual screen will run first.

I would like to ask:

  1. Whether it is possible to allow the IDE to be able to generate the same event handlers for both screens regardless of whether it exists in one another (similar to how CUBA Studio allows it)
  2. How can we control the “ordering” of the event handlers such as having 2 onInits / onBeforeShows / onAfterShows ?
1 Like

Hi,

Listeners are triggered in the order they are added. But, in case of adding listeners using a declarative approach, i.e., using the @Subscribe annotation, we cannot guarantee the order in which they are added.

The studio doesn’t suggest adding a listener of the same type, added using @Subscribe if one already exists in the view. Instead, it’s better to override the existing method to run your code before or after the base logic.

To make default listeners trigger before the user’s custom ones, add them in the constructor of the base class, just like we do with our base classes:

public StandardListView() {
    addBeforeShowListener(this::onBeforeShow);
    addReadyListener(this::onReady);
}

private void onBeforeShow(BeforeShowEvent event) {
    setupSaveShortcut();
}

private void onReady(ReadyEvent event) {
    setupLookupComponent();
}

Regards,
Gleb

1 Like