Question

Ok, here is my dilemma:

I got a Header presenter which is the nested presenter. The Customer presenter is the child of Header presenter (ie the Customer presenter was put into a slot of Header presenter).

There is Login Panel in Header presenter.

-So here is the requirements:

-Users can not see any Gui of Customer presenter if they have not logged in yet.

-When the user logins successfully then it will visible the Gui of Customer presenter.

-When User refreshes the page, the system will check if the customer logined. If he/she did login, then the system will show Gui of Customer presenter. If he/she has not logined, then prompt an error message.

So here is what I did.

In Header presenter, I have a method loginedSuccessfully(), this method will passUserInfo() (use eventBus) into Customer presenter.

In Customer presenter, I have:

private int custID=0;
public void onReset(){
   super.onReset();
   if(custID>0){
       showGui();
   }
   else{
       hideGui(); // the hideGui() has Window.alert("Pls Login");
   }
}
private PassUserInfoHandler passUserInfoHandler=new PassUserInfoHandler(){

    @Override
    public void onPassUserInfo(PassUserInfoEvent event) {
       custID=event.getCustID();
       if(custID>0){
           showGui();
       }
       else{
          hideGui();
       }
    }

};

When user refresh the Customer page. The onResetwill be called, if customer has not logined yet then it won't show Gui & prompt a message.

When user logins by clicking login button then passUserInfoHandler will be called & it will show the Gui if logged in ok.

But here is the problem. When user already logged in & if I open a new browser Tab & re-open the Customer page, then this time both onReset & passUserInfoHandler waere called & the page prompts the message TWICE. This is not Good.

But why that happened?

This is because the onReset was called before the passUserInfoHandler was called. That is why checking gui method was called 2 times.

I used Scheduler.get().scheduleDeferred(new Command() {}; but it didn't work.

Do you know how to fix the problem so that it can meet all the above requirements?

Was it helpful?

Solution

GWTP is full fledge architecture. It's also have such API that, you can prevent some presenters from revealing themselves by creating your custom Gatekeeper classes. For example, if you want some presenters to be accessible only when the user is logged in you could write the following class:

@Singleton
public class LoggedInGatekeeper implements Gatekeeper {  

  private final CurrentUser currentUser;

  @Inject
  public LoggedInGatekeeper (
      final CurrentUser currentUser ) {
    this.currentUser = currentUser;
  }

  @Override
  public boolean canReveal() {
    return currentUser.isLoggedIn();
  }
}

Then you simply need to add the @UseGatekeeper annotation to the proxy of each presenter you want to protect:

  @ProxyCodeSplit
  @NameToken("userSettings")
  @UseGatekeeper( LoggedInGatekeeper.class )
  public interface MyProxy extends ProxyPlace<MainPagePresenter> {}

I think you should go for it, instead of write manually stuff. Even once may be your code work. but in feature some new bug will be raise in your code. you can refer this GWTP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top