Instantiate a PresenterWidget (GWTP) manually

2019-02-26 02:45发布

问题:

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