Wednesday, June 30, 2010

Re: GWT / GIN: Adding user class to all presenters

Hello,

You can bind User object to a singleton provider that is retrieving user
information. Here is what we are doing in one of our projects:

public class UserInfo {

public static UserInfo with(String email, String firstname, String
surname, String family) {
UserInfo info = new UserInfo();
info.email = email;
info.firstname = firstname;
info.surname = surname;
info.family = family;

return info;
}

private UserInfo() {

}

private String email;
private String firstname;
private String family;
private String surname;

public String getEmail() {
return email;
}

public String getFirstname() {
return firstname;
}

public String getFamily() {
return family;
}

public String getSurname() {
return surname;
}
}


and also we have a provider class:

public class UserInfoProvider implements Provider<UserInfo> {

public UserInfo get() {
Dictionary userInfoDict = Dictionary.getDictionary("userInfo");

String email = userInfoDict.get("email");
String firstname = userInfoDict.get("firstname");
String surname = userInfoDict.get("surname");
String family = userInfoDict.get("family");

UserInfo userInfo = UserInfo.with(email, firstname, surname, family);
return userInfo;
}
}

As you can see the provider is retrieving user information using
Dictionary provided by GWT. Currently our main page is rendering user
information as an in-line json that looks as follow:
<script language="JavaScript">
var userInfo = {
email: "johnuser@somedomain.com",
firstname: "John",
surname: "",
family: "Smith"
}
</script>

And finally we are adding binding to the provider class:
bind(UserInfo.class).toProvider(UserInfoProvider.class).in(Singleton.class);
// we are binding it as a singleton, cause we don't want to instantiate
new UserInfo object each time when user info have to be rendered.

And here is a sample usage:

public class UserDetailsEditor {

@Inject
public UserDetailsEditor(UserInfo userInfo) {

}
}


Regards,
Miroslav
On 06/29/2010 06:23 PM, jmccartie wrote:
> I have a hazy understanding of GIN, but have it working for injecting
> presenters, etc.
>
> I'm trying to inject a self-made "User" class into all my presenters
> in order to get the currently logged-in user.
>
> I've added @Inject to the constructor on my User class, and added User
> to my GIN module ... but apart from that, I'm totally lost. Do I bind
> it to my app presenter (tried that, but I get an error since User
> doesn't extend my AppPresenter)? As a singleton? Is this even the
> right way to get pass this data around?
>
> I hate to post this here (not looking for free homework), but I can't
> find a decent tutorial/example on this anywhere else.
>
> Much thanks in advance.
>
>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment