On 24 juin, 12:01, "[email protected]" <[email protected]>
wrote:
> Hi all.
>
> I've been trying to get a nice setup of the best practices presented
> at google i/o. I have something working, but a few questions.
>
> The first one is how fine-grained do you go for the events you define?
> Say i have events for updates in entities A and B in my model:
> UpdatedAEvent, UpdatedBEvent. To prevent creating new event classes
> for every entity i might add, i liked the idea of using generics after
> looking at the source code of GWT.
>
> Classes like:
> public class ModelUpdatedEvent<V> extends
> GwtEvent<ModelUpdatedHandler<V>> {
> ...
> private static Type<ModelUpdatedHandler<?>> TYPE;
> ...
> public static Type<ModelUpdatedHandler<?>> getType() {
> if (TYPE == null) {
> TYPE = new Type<ModelUpdatedHandler<?>>();
> }
> return TYPE;}
I'd rather write:
@SuppressWarning("unchecked")
public static <V> Type<ModelUpdatedHandler<V>> getType() {
return TYPE;
}
(TYPE might have to be of type Type<?>)
(Given that you most probably won't use ModelUpdatedEvent without its
TYPE, you can just initialize it in the declaration, like GWT does,
and have getType() just "return TYPE")
> But then i guess i can't use them with an event bus (the
> HandlerManager)? I lack complete understand of generics and wildcards,
> but assuming the class doesn't know the type, this won't work right?
Even if the class did use "V", it wouldn't work, because generics in
Java are hardly more than just syntactic sugar (google for "java
generics erasure").
> eventBus.addHandler(ModelUpdatedEvent.getType(), ...)
However you write ModelUpdatedEvent/ModelUpdatedHandler, you'd end up
having all your handlers receive all ModelUpdatedEvent<?> events.
The only way you could make this work is having a getType(Class<?>
cls) returning a different Type<ModelUpdatedHandler<?>> instance for
each class given as argument:
public static <V> Type<ModelUpdatedHandler<V>> getType(Class<V> cls) {
// do your magic here
}
...
eventBus.addHandler(ModelUpdatedEvent.getType(A.class), ...);
...or you could instead have a getTypeForA() and getTypeForB()
methods:
public static Type<ModelUpdatedHandler<A>> getTypeForA() { return
TYPE_A; }
public static Type<ModelUpdatedHandler<B>> getTypeForB() { return
TYPE_B; }
...this wouldn't be that different from having a ModelUpdatedEvent
subclass per "model class".
> The second point is regarding dependency injection. I got it working
> for things implementing interfaces, to which i bind implementations.
> But how do you go for the HandlerManager for example? I've tried
> making it a singleton as in:
> protected void configure() {
> ...
> bind(HandlerManager.class).asEagerSingleton();
> ...
>
> but i get "No @Inject or default constructor found for class
> com.google.gwt.event.shared.HandlerManager".
>
> Same if i use .in(Singleton.class). I've seen that GWT should fallback
> to GWT.create when there's no injection available, but if i don't
> mention the HandlerManager in my injector it also fails.
...and GWT.create() expects a default constructor.
The solution IMO is to use a provider.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---