GWT singleton widget being removed from DOM after injection

92 views Asked by At

I have a shared GWT widget "HeaderView" which is a header for all views. I instantiate it through GIN via -

bind(HeaderView.class).asEagerSingleton();

I can then inject into my views like so -

@Inject
public DashboardViewImpl(HeaderView headerView) {
    this.headerView = headerView;
    initWidget(dashboardViewUiBinder.createAndBindUi(this));
}

This works fine for the first time a view is displayed. However, if I change places and then go back to a view that has already been displayed once the "HeaderView" is no longer visible. Inspecting the HTML shows that it is no longer attached to the DOM.

My views are bound as singletons -

bind(DashboardView.class).to(DashboardViewImpl.class).in(Singleton.class);

I am assuming that the HeaderView is being removed from the DOM after loading the next page due to widgets only being allowed to have a single parent.

Is there a correct way to share a widget between all views in a GWT application?

1

There are 1 answers

0
Andrei Volgin On BEST ANSWER

First of all, a compiler is pretty good at optimizing code, so making a widget singleton is not going to save much in terms of code size. It does improve the performance, but it won't be noticeable unless the widget is very complex.

In your case the header should not be part of any view. You should add it directly to your app widget, i.e. the one you add to the RootPanel. Then your "views" will occupy the remaining part of the browser window. You can still tell your header what it needs to know when a user navigates from one view to the next.