I am using gwt-platform, objectify and appengine (java).
I am trying to create a user and session management system after best practices. I would like this to be a reusable module in applications.
I have, in my application, based on the Dispatch module in gwt-platform, a Login action and a Logout one.
When I enter the application, the login screen appears and everything works fine. If I navigate between presenters and after that I press the Logout button, I see that the logout action is executed multiple times, or any other Action for that matter. The number of executions for an Action ( which should only run once ) seems to be directly proportional with the number of times I navigate between presenters.
The source code for the project is hosted at: http://code.google.com/p/gwt-platform-appengine-session-login-example/
If someone could give me some hints of what is wrong it would be great. Also, if someone would also like to contribute to the project,some help would be appreciated. Please email me.
The problem is that you add your EventHandler for the Logout button in the onReset()
method.
However if you check the GWTP docs the onReset()
method is called whenever you navigate to the presenter. So if you navigate to the presenter multiple times the EventHandler
will be added multiple times and thus executed multiple times (directly proportional to the number of times you open the presenter).
Move the EventHandler
out of the onReset()
method into the onBind()
method because onBind()
is only called once in the presenter's life cycle and it is the place where you should add EventHandlers
. In addition to this wrap it into registerHandler()
method so it is automatically removed when the onUnbind()
is called.
FirstApplicationPresenter.java:
@Override
protected void onBind() {
super.onBind();
getView().getLogoutBtn().setText("Logout "+username);
registerHandler(getView().getLogoutBtn().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Logout action = new Logout();
dispatchAsync.execute(action, logoutCallback);
}
}));
}
@Override
protected void onReset() {
super.onReset();
}