gwtgwtpgwt-platform

GWTP how to pass PlaceManager object to view?


I have view, that communicates with server to upload and download file.

I've added EventHandler for Unauthorized response from server to my view.

Now I would like to redirect user to login page:

PlaceManager pm;
PlaceRequest placeRequest = new PlaceRequest.Builder(pm.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
pm.revealPlace(placeRequest);
MaterialToast.fireToast("You session has expired.");

but for this I need PlaceManager object. How can I inject it to my view?

I use GWT 2.7 and GWTP


Solution

  • First of all you shouldn't have any of this logic in the view. Instead you should create

    public interface YourViewUiHandlers extends UiHandlers{ 
        void navigate();
    }
    

    Then change MyView interface to implement HasUiHandlers<YourViewUiHandlers> and in the ViewImpl replace extends ViewImpl with extends ViewWithUiHandlers<YourViewUiHandlers >

    Now in your view you'll be able to call your presenter like this:

    getUiHandlers().navigate();
    

    Of course your presenter should

    1. Implement YourViewUiHandlers
    2. In it's contructor do something like view.setUiHandlers(this);

    PlaceManager can be just injected into Presenter with @Inject annotation. Actually if you want to make it bad and dirty @Inject will do it for the view as well. And if it's not View but just some composite - Initialize it with GIN using @UiField(provided=true), and then simply inject it in the constructor/set the field before initWidget();