How to call backend methods from JS independent from Vaadin View?

Michael Kemmerzell :

I have the following scenario. Javascript is executed in the frontend and reacts on events. If an event is catched a specific backend method is called.

I initialize the backend like the following. The method is called during the initialization of one of my Vaadin 14 views.

window.init = function(element) {
    window.element = element;
    ...
}

View:

public class TestView extends Div {
   public TestView() {
      ...
      UI.getCurrent().getPage().executeJs("init($0)", this);
   }
}

later on the methods are called with window.element.$server.onTestEvent(); on the frontend.

The called backend method:

@ClientCallable
private void onTestEvent() {
    System.out.println("Test");
}

Now the issue:

I pass the view as element.

If I change the view the element I passed is destroyed so the event (on the backend side) can't be executed even if it is catched on the frontend.

Does Vaadin 14 provide a way to store objects "globally" over all views? I want the application to react on the events independent on which view I currently visit.

ollitietavainen :

The easiest solution would be to use the element of a top-level main layout (view container / MainView) instead of individual views' elements.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342046&siteId=1