Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
CDIUI + CDI-helpers with CDI events
I'm having problems getting CDI events to work properly with CDI-Helpers.
I have a ViewMenuUI with two Views, both UIScoped. Using the menu, I can correctly navigate between the two views. However, when I fire a CDI event from the one view with the corresponding observer in the other view, instead of using the exisitng view object, it creates a new view bean, calls the post construct on that bean, and then calls the observer method.
Has anyone successfully done this before, or have any tips on how this can be done please.
I eventually figured out why this does not work.
The problem is not limited to the CDI-Helpers addon but occurs also in standard CDIView classes. For some reason i dont understand, the @Observer function cannot be in the CDIView class. If the observer function is placed in a component that in turn is injected into the CDIView class, it all works fine.
Perhaps a CDIView is not a proper CDI bean?
Warning I'm not an expert in cdi, but here goes.
CDIView is @ViewScoped, the observer view is not in the same scope as the one that fires the event.
if the observer method is non-static, the container must:
Obtain a contextual instance of the bean which declares the observer method according to Contextual instance of a bean. If this observer method is a conditional observer method, obtain the contextual instance that already exists, only if the scope of the bean that declares the observer method is currently active, without creating a new contextual instance.
So you are creating an instance of the Observer view for the same scope as the event originator so that cdi can call the observer method on the contextual instance. There might be some cdi magic tricks with qualifiers to change the behaviour, but I simply don't know the spec well enough to pull one out of the hat.
In a geniric case it might be better to use the classic Listener/Event Pattern implementation to comunicate between the views.
If you have only one of each view in the ui ( as is the case with CDIViews usually ) you could probably observe the event in the UI and delegate it from there to the observer view.