2012-04-12 3 views
4

私はthis tutorialの推奨に従ってGWTアプリケーションを実装しました。私もサンプルコードをダウンロードしました。デモのすべてのプレゼンターは、インスタンス化される主プレゼンターコンストラクターに注入する必要があることに気付きました。著者はソースでこれを含める:GWT/Gin 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に注入されない場合、それはインスタンス化されないことを確認しました。デモンストレーションの目的では、アプリ全体に2人のプレゼンターしかいないので、これはクールですが、実際のアプリケーションでは、これは重大な不便なことになります。

Presenterのインスタンス化を行う適切な方法は何ですか?

プレゼンターモジュール:

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(); 

} 

答えて

2

私はあなたがあなたのGinjectorとが欠けていると思う

EDIT:参照用のGIN関連クラスを含む AbstractGinModule definiそのサンプルから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(); 
    } 
} 
+0

ソースコードをダウンロードしてテストしたところ、あなたが言及したクラスが含まれていました。それでも、GreetingResponsePresenterが機能するためには、「FUDGE FACTOR」が必要でした。S –

3

ますプロバイダを使用してこれを解決できます。私のジンモジュールでは、私はそれと一緒に私のプレゼンター/アクティビティを宣言するが、この

// Content Area 
    bind(ContentActivityMapper.class).in(Singleton.class); 

    // Intro Page 
    bind(IntroPageActivity.class); 
    bind(IntroPageView.class).to(IntroPageViewImpl.class).in(Singleton.class); 

ようなビューは、その後の活動マッパーで私はwithPlace方法に設定している。この

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を引数なしで挿入できるようにします。そのコードは次のようになります。

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()); 
    }... 
+0

使用されているMVPフレームワークには、Presenter/Displayインジェクション用の機能がすでにあります(別の回答を参照)。しかし、私たちは、アプリケーションの主なプレゼンターの議論としてすべてのプレゼンターが必要です。S –

+0

この例では表示しませんでしたが、このアプリケーションでは約20の異なる活動/プレゼンターがあります。私はマイムのプレゼンターにそれらを埋め込む必要はありません。これは私が必要とするもので、必要に応じて作成され、コード分割のためのAsyncProviderクラスでも動作します。 – Deanna

+0

私はGWTPでこれをしようとしていますが、私のプロバイダは常に未定義です。親Presenterは '@Injected Provider myAbc'を宣言していますが、何かが見付かりません。どのような問題が起こっているのでしょうか? – displayname

関連する問題