Instantiate a PresenterWidget (GWTP) manually

眉间皱痕 提交于 2019-12-01 22:06:46

问题


i am new to GWT, GWTP.

I have a PresenterWidget which gets instantiated multiple times and gets added to a slot. I want to define the amount of instances programmatically. I only know that I make a new Instance of the Widget by Injecting it.

@Inject MyWidgetPresenter first; 
@Inject MyWidgetPresenter second; 
// ...

Can I instantiate it with new? how do instantiate the view?

I tried to use a composite because it seemed to be the smartest solution, because it doesnt contain much logic anyway. But I cant add a composite to a slot.

Thanks in advance


回答1:


You can get the ClientGinjector reference and call a getYourPresenter() method. If it's declared as @Singleton it will return the same instance, if is not will create a new instance, same as using new, but injecting all parameters to the constructor. It will be the same as adding @Inject to a field.

Added: If you don't have the required presenter there, at your ClientGinjector, you can simply write a getter method at that interface. Ex:

public interface ClientGinjector extends Ginjector {
    ....
    SideMenuPresenter getSideMenuPresenter();
    ....
}



回答2:


In GWTP 1.0 the Ginjector is not needed anymore (auto-generated by gwtp).

In your case (and each time you need multiple instance of a class) the best is to inject a Provider of your WidgetPresenter:

@Inject Provider<MyWidgetPresenter> myWidgetPresenterProvider; 

Each time you need a new instance of MyWidegetPresenter, call the get() method on the provider :

MyWidgetPresenter myWidgetPresenter = myWidgetPresenterProvider.get();


来源:https://stackoverflow.com/questions/17948751/instantiate-a-presenterwidget-gwtp-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!