MVP + GIN + Storing User Data in client Code?

44 views
Skip to first unread message

vehdra music

unread,
Aug 28, 2011, 9:34:06 PM8/28/11
to google-we...@googlegroups.com
Hi, have a few doubts about using mvp + gin.

One thing that I want to do is this. I want that when my app loads (in the entry point) load the logged user data and display one or another gui (one for the root users and other for the normal users).

Obviously in the server I check if the user has the right rol to perform the action requested via RPC.

This are my doubts:

1. To get the user data, is the best way to do something like this:

public void onModuleLoad() {

    ginjector.getUserServiceAsync().getCurrentUser(new AsyncCallback<UserModel>() {

        public void onSuccess(UserModel result) {

            // store user data somewhere

            // display the right gui depending on the user rol

        }

        public void onFailure(Throwable caught) {

            // do some

        }

});

2. To store in client code the user data, I was trying to do something like Thomas said here http://stackoverflow.com/questions/5827534/cross-activity-references-using-gwt-with-gin 

I've created a CurrentUserProvider class and i've binded my UserModel to my CurrentUserProvider. And in my Ginjector i've created a method 

CurrentUserProvider getCurrentUser();

Then in my rpc callback I put the the user data in the provider.

ginjector.getUserServiceAsync().getCurrentUser(new AsyncCallback<UserModel>() {

        public void onSuccess(UserModel result) {

            // store user data somewhere

            ginjector.getCurrentUser().setCurrentValue(result);

        }

      .......

It works in some kind, but I don't see it so clean. I believe that it could be done in a nicer way. Can you please give any idea about how are you handling this in your projects?

On the other side I have the trouble about displaying one or another gui depending on the user's rol.

In my Ginjector I have this method AppLayoutView getAppLayout(); that returns me my applayout. Basically, when the user is root haves one menu, and when the user is not root, haves another menu. Show I create my menu by code and do something like this in the view?

if (currentUserProvider.get().getRol().equals(ROLE_ROOT)) {

    // create one menu

} else {

   // create normal user menu

}

And finally... I have a MvpModule (GinModule) where I have this provider:

@Provides 

  @Singleton

  public PlaceHistoryHandler getHistoryHandler(PlaceController placeController, PlaceHistoryMapper historyMapper, EventBus eventBus) {

    PlaceHistoryHandler historyHandler =  new PlaceHistoryHandler(historyMapper);

    historyHandler.register(placeController, eventBus, new CompaniesListPlace());

    return historyHandler;

  }

Where I want to get the user to right place, again, depending on the user's role. I've tried doing something like this:

@Provides 

  @Singleton

  public PlaceHistoryHandler getHistoryHandler(PlaceController placeController, PlaceHistoryMapper historyMapper, EventBus eventBus, CurrentUserProvider currentUserProvider) {

    PlaceHistoryHandler historyHandler =  new PlaceHistoryHandler(historyMapper);

    if (currentUserProvider.get().getRole().equal(BLA)) {

         historyHandler.register(placeController, eventBus, new CompaniesListPlace());

    else {

         // go to another place

    }

    return historyHandler;

  }

But what is happening to me is that this method is being called before that my rpc returns me the user data. So currentUserProvider is not initialized.

How can I handle this?


Any help would preciated!

Alex Dobjanschi

unread,
Aug 29, 2011, 3:28:07 AM8/29/11
to google-we...@googlegroups.com
You should not build your Ginjector until you read user role/permissions.

App {
 show_some_loading_ui ();
 rpc_get_user_and_permissions (callback {
   success (..) {
     // hide some_loading_ui.
     // use a DeferredCommand to load the app (#see onModuleLoad2 -P).
   }

   failure (..) {
     // show guest?
   }
 });
}
Reply all
Reply to author
Forward
0 new messages