我按照this tutorial的建议实施了GWT应用程序。我还下载了示例代码,我注意到演示的每个演示者都需要在主要的presenter构造函数中注入以进行实例化。作者将此包含在源代码中:
public class GreetingPresenter extends WidgetPresenter<GreetingPresenter.Display> {
// FUDGE FACTOR! Although this is not used, having GIN pass the object
// to this class will force its instantiation and therefore will make the
// response presenter listen for events (via bind()). This is not a very good way to
// achieve this, but I wanted to put something together quickly - sorry!
private final GreetingResponsePresenter greetingResponsePresenter;
@Inject
public GreetingPresenter(final Display display, final EventBus eventBus, final DispatchAsync dispatcher, final GreetingResponsePresenter greetingResponsePresenter) {
super(display, eventBus);
this.dispatcher = dispatcher;
this.greetingResponsePresenter = greetingResponsePresenter;
bind();
}
我验证了如果没有在GreetingPresenter中注入任何Presenter,它将不会被实例化。出于演示目的,这很酷,因为整个应用程序只有两个演示者,但在真实世界的应用程序中,这可能会非常不方便。
完成Presenter实例化的正确方法是什么?
编辑:包括GIN相关课程供参考:
演示者模块:
public class GreetingClientModule extends AbstractPresenterModule {
@Override
protected void configure() {
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(PlaceManager.class).in(Singleton.class);
bindPresenter(GreetingPresenter.class, GreetingPresenter.Display.class, GreetingView.class);
bindPresenter(GreetingResponsePresenter.class, GreetingResponsePresenter.Display.class, GreetingResponseView.class);
bind(AppPresenter.class).in(Singleton.class);
bind(CachingDispatchAsync.class);
}
}
Ginjector:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}
答案 0 :(得分:3)
您可以使用提供商解决此问题。在我的Gin模块中,我声明了我的Presenter / Activity以及它的视图
// Content Area
bind(ContentActivityMapper.class).in(Singleton.class);
// Intro Page
bind(IntroPageActivity.class);
bind(IntroPageView.class).to(IntroPageViewImpl.class).in(Singleton.class);
然后在Activity Mapper中我使用像这样的提供者
public class ContentActivityMapper implements ActivityMapper {
@Inject Provider<IntroPageActivity> introPageProvider;
public Activity getActivity(Place place) {
if (place instanceof DefaultPlace) {
return introPageProvider.get().withPlace(new IntroPagePlace());
}...
我在ActivityPresenter上设置了withPlace方法,允许在没有参数的情况下注入它。该代码看起来像这样。
public class IntroPageActivity extends AbstractActivity {
private IntroPageView view;
@Inject
public IntroPageActivity(IntroPageView view) {
super();
this.view = view;
}
public IntroPageActivity withPlace(IntroPagePlace place) {
return this;
}
@Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
containerWidget.setWidget(view.asWidget());
}...
答案 1 :(得分:2)
我认为您缺少Ginjector和AbstractGinModule定义,您可以从该样本中定义GIN绑定。
我建议您从上述教程中下载code,并查看GIN入门wiki。
<强> GreetingGinjector 强>:
@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
public interface GreetingGinjector extends Ginjector {
AppPresenter getAppPresenter();
PlaceManager getPlaceManager();
}
<强> GreetingClientModule 强>:
public class GreetingClientModule extends AbstractPresenterModule {
@Override
protected void configure() {
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(PlaceManager.class).in(Singleton.class);
bindPresenter(GreetingPresenter.class, GreetingPresenter.Display.class, GreetingView.class);
bindPresenter(GreetingResponsePresenter.class, GreetingResponsePresenter.Display.class, GreetingResponseView.class);
bind(AppPresenter.class).in(Singleton.class);
bind(CachingDispatchAsync.class);
}
}
<强>入口点强>:
public class GreetMvp implements EntryPoint {
private final GreetingGinjector injector = GWT.create(GreetingGinjector.class);
public void onModuleLoad() {
final AppPresenter appPresenter = injector.getAppPresenter();
appPresenter.go(RootPanel.get());
injector.getPlaceManager().fireCurrentPlace();
}
}