简体   繁体   English

GWT单例小部件在注入后从DOM中删除

[英]GWT singleton widget being removed from DOM after injection

I have a shared GWT widget "HeaderView" which is a header for all views. 我有一个共享的GWT小部件“ HeaderView”,它是所有视图的标题。 I instantiate it through GIN via - 我通过GIN通过-

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. 但是,如果我更改了位置,然后返回到“ HeaderView”不再可见的视图,则该视图已经显示。 Inspecting the HTML shows that it is no longer attached to the DOM. 检查HTML显示HTML不再附加到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. 我假设由于小部件只允许有一个父级,因此在加载下一页后,HeaderView将从DOM中删除。

Is there a correct way to share a widget between all views in a GWT application? 是否存在在GWT应用程序的所有视图之间共享窗口小部件的正确方法?

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, ie the one you add to the RootPanel. 您应该将其直接添加到您的应用程序小部件,即您添加到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. 您仍然可以告诉标题,当用户从一个视图导航到下一个视图时,它需要知道什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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