如何正确配置我的 GWT 活动和活动映射器?



我有以下类:

// The "backing bean" for DashboardDisplay.ui.xml (which is omitted for brevity).
public class DashboardDisplay extends Composite implements AcceptsOneWidget {
private EventBus eventBus;
private PlaceController placeController;
private IsWidget content = null;
public DashboardDisplay(EventBus eventBus, PlaceController placeController) {
super();
setEventBus(eventBus);
setPlaceController(placeController);
}
// Getters and setters for all properties
// ...
@Override
public void setWidget(IsWidget widget) {
setContent(widget);
}
}
public class DashboardActivity extends AbstractActivity {
private AcceptsOneWidget container;
private ActivityManager manager;
private DashboardDisplay display;
public DashboardActivity(AcceptsOneWidget container, ActivityManager manager,
DashboardDisplay display) {
super();
setContainer(container);
setManager(manager);
setDisplay(display);
// I *believe* this ensures that the 'panel' passed in to the
// start method is always our 'container'. Yes?
manager.setDisplay(container);
}
// Getters and setters for all properties
// ...
@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
display.setEventBus(eventBus);
panel.setWidget(display);
}
}
public class DashboardActivityMapper implements ActivityMapper {
private DashboardActivity activity = null;
// Getters and setter for activity.
@Override
public Activity getActivity(Place place) {
// Leaving arguments null because I'm confused about the object graph
// (read below).
return new DashboardActivity(null, null, null);
}
}

因此,我开始对 Activity 框架有所了解,但对它仍然很陌生,以至于当我尝试远离Hello, GWT示例时,我几乎立即遇到了 API 的障碍。

在我的应用程序的主 GWT 模块MyWebAppModule中,我正在尝试实例化和配置这 3 个对象。但是因为我仍然不确定如何使用他们的 API,所以我遇到了实现问题:

public class MyWebAppModule implements EntryPoint {
@Override
public void onModuleLoad() {
// I'll worry about DI and getting GIN to work after
// I figure this basic stuff out.
EventBus eventBus = new SimpleEventBus();
PlaceController placeController = new PlaceController(eventBus);
MyWebAppPlaceHistoryMapper placeMapper = new MyWebAppPlaceHistoryMapper();
HomePlace homePlace = new HomePlace();
// WHERE THE PROBLEM STARTS
DashboardActivityMapper activityMapper = new DashboardActivityMapper();
ActivityManager manager = new ActivityManager(activityMapper, eventBus);
// Really not sure how to inject/configure this properly.
DashboardActivity dashboardActivity = new DashboardActivity(null, null, null);
// WHERE THE PROBLEM ENDS
PlaceHistoryHandler historian = new PlaceHistoryHandler(placeMapper);
historian.register(placeController, eventBus, homePlace);
historian.handleCurrentHistory();
}
}
  1. 显然,我没有正确实现DashboardActivityMapperDashboardActivity,因此当我尝试在MyWebAppModule#onModuleLoad中实例化和连接它们的实例时,它会导致问题。有人可以帮助我正确实现这一点吗?一旦我得到一个工作示例,我相信我将能够连接活动框架中的其余"点"。
  2. 除非我遗漏了什么,否则上面的ActivityManager manager似乎没有添加到任何其他类中。这是对的吗?GWT 是否会确保它在内存中保持活动状态,以便在我传递它PlaceChangeEventeventBus始终侦听?还是我需要将其添加/注册到任何内容?

提前感谢!

  1. 您的DashboardActivityMapper负责在到达给定位置(例如HomePlace)时创建和返回DashboardActivity。这是您缺少的信息吗?
    见 https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces#ActivityMapper

  2. 只要EventBus具有ActivityManager的句柄(以便它可以向其调度事件)。但是,当使用非 null 值调用时,ActivityManager在其setDisplay方法中在EventBus上注册自身(并在将null传递给setDisplay时取消注册自身),并且您不调用setDisplay(顺便说一下,这意味着活动甚至不会显示)

相关内容

  • 没有找到相关文章

最新更新