简体   繁体   中英

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

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.

最简单的解决方案是使用顶级主布局(视图容器/ MainView)的元素而不是单个视图的元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM