building custom event differently to avoid boiler plate coding?

67 views
Skip to first unread message

Elhanan

unread,
Jun 20, 2011, 8:52:29 AM6/20/11
to google-we...@googlegroups.com
hi..

i'm re-posting this, as i'm not sure the original post was ... posted, as i havn't goined the group when i wrote it..
..
after reading about custom events in gwt i understand that for each custom events you must:
1. define an event handler which events EventHandler
2.  create an event class which extends Event
3. define a static Type<MyHandler> inside the event (return that type on the method)
4. add the handler to the event bus
5. fire the event

BUT
why shouldn't i do the following:
1. create a generic eventHandler interface like so:
public interface GenericEventHandler<P>  extends EventHandler{
 public void onEvent(P p);
}
create an event factory which extends type

public class EventFactory<P> extends Event.Type<
GenericEventHandler<P>>
{
    public Event create(P p){
      return new Event(){
      public Type<GenericEventHandler<P>> getType(){
   return MessageFactory.this;
     }
   public dispatch (GenericEventHandler h){
   h.onEvent(p);
  }
}
}
}

from then on all i would need to do this for each new event

final static EventFactory<String> myEvent1 =new EventFactoy<String>();
eventbus.addHandler(myEvent,new GenericEventHandler<Sting>(){
..
}
fire( myEvent1.create("sds"));

Ramon Salla

unread,
Jun 21, 2011, 8:28:15 AM6/21/11
to google-we...@googlegroups.com
+1 to this!

Elhanan

unread,
Jun 21, 2011, 8:49:06 AM6/21/11
to google-we...@googlegroups.com
thanks, i was thinking about maybe starting a blog called "the no-boilerplate series" to post shortcuts such as these i come with, but i thought surely folks have more experience then me, in these things..

Alex D.

unread,
Jun 21, 2011, 3:46:54 PM6/21/11
to google-we...@googlegroups.com
You shouldn't use custom events for every possible event in your app, imo -- or, even better, identify the best events that match as much as possible.

What you're suggesting by this is removing the strong-type event definitions provided by Java.

Of couse, you can work with a class called Event, that merely has a "String name" and "Object data", but this defeats the purpose of what GWT is suggesting

Elhanan Maayan

unread,
Jun 21, 2011, 11:03:45 PM6/21/11
to google-we...@googlegroups.com
first off  you probably mean the strong type definitions provided by GWT, not java right?
 
second, that's not what i'm suggesting, i don't want to use events for enable disable buttons, but i  do want to use to create logical messages, that a work process has ended/started, or basically whatever causes you to leave a view and enter another one.  
 
i'm also not working with just Object or string, i'm doing exactly the same thing gwt does only with less code, i'm providing strong typed events, with generics, only without the need to actually create a new class for it every single time
 
i can do this:
 
private final MessageFactory<Entity> ENTITY_CREATED=new MessageFactory<Entity>()
private final MessageFactory<Entity> ENTITY_DELETED  =new MessageFactory<Entity>();
 
...
addHandler(ENTITY_CREATED=new MessageHandler<Entity>{
}
 
fire(ENTITY_CREATED.fire(entity))
 
i get a strong typed custom event just like before, only with much less code..

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/Y_ZKLyl29DkJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

lemaiol

unread,
Jul 4, 2011, 2:08:48 PM7/4/11
to Google Web Toolkit
I just published an article on this topic. Maybe you want to have a
look at it: http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/

Feedback is welcome!

cheers,
Berto
Reply all
Reply to author
Forward
0 new messages