Hi Thomas, Olivier,
Thanks to both of you for the help.
So, the reason why I don't want to go through the EventBus is that we are
building a
big application, several screens, and there are quite a few events going on.
The EventBus
should be used for application-wide event, imho, and not for children-parent
communication.
That's why I was looking for something alternative. One way would have been
to expose the
parent to the children, and let the children directly call the parent. But I
don't like this, the
coupling is too strong (e.g., the children can't exist without the parent),
and the granularity of
the comunication is coarse. I want to avoid strong coupling and have
fine-graine comunication,
and this is what I came up with... which kind of goes in what you were
suggesting, but I miss
to see the strong coupling you are talking about... to me is rather the
other way around, so if you
could assist me and elaborate why I'm getting to strong coupling, please let
me know.
Furthermore, if I ever change my mind, passing the event on the EventBus
rather than the local
HandlerManager is going to be a very quick, and painless, thing to do.
Below the skeleton of what I've done:
Begin example
----------------------------------------------------------------------------------------------------------------
/** children presenter that want to handle local event implements this */
public interface HasLocalEventHandlers {
<H extends EventHandler> HandlerRegistration addHandler(Type<H>
type, H handler);
}
/** parent presenter */
class ParentPresenter extends ... {
private HasLocalEvents child;
onBind() {
registerHandler(child.addHandler(DeleteEvent.getType(), new
DeleteOfferEvent.DeleteHandler() {
@Override
public void onDelete(final DeleteOfferEvent event) {
// react on child event
}
}));
}
}
/** child presenter */
class ChildPresenter extends ... implements HasLocalEventHandlers {
Display {
HasClickHandler delete();
}
private final HandlerManager localHandlerhandlerManager ;
@Inject
public ChildPresenter(...) {
super(...);
this.localHandlerhandlerManager = new HandlerManager (this);
}
onBind() {
registerHandler(this.getDisplay().detele().addClickHandler(new
ClickHandler() {
@Override
public void onClick(ClickEvent e) {
fireEvent(new DeleteEvent());
}
}));
}
private void fireEvent(GwtEvent<?> event) {
if (localHandlerhandlerManager != null) {
localHandlerhandlerManager.fireEvent(event);
}
}
public final <H extends EventHandler> HandlerRegistration addHandler(
Type<H> type, H handler) {
return localHandlerhandlerManager.addHandler(type, handler);
}
}
/** Delete Evenet */
public class DeleteEvent extends GwtEvent<DeleteEvent.DeleteHandler> {...}
End example
----------------------------------------------------------------------------------------------------------------
On Fri, Jan 29, 2010 at 10:30 AM, Thomas Broyer <[email protected]> wrote:
>
> On Jan 29, 8:46 am, Mirco Dotta <[email protected]> wrote:
> > Hi,
> >
> > This is a question I've been asking myself several times, here is the use
> > case:
> >
> > I've got an action happening in a ChildPresenter, such as a
> > DeleteElementEvent.
> > The ParentPresenter would like to register to this type of events so that
> a
> > correct
> > reaction can be triggered.
> >
> > Now, a possible way to go is to pass the Event in the EventBus and having
> > the ParentPresenter
> > registering on the EventBus for such events. This is going to work, but I
> > really don't like this
> > solution as the Event is not application-wide and therefore should not be
> > polluting the event bus.
> >
> > I'd rather have a local event, but how can I achieve this? Should I
> create a
> > HandlerManager
> > it in the ParentPresenter... but then hwo can I pass it to the children
> (GIN
> > injection?!, but how?).
> >
> > I'm looking for a pattern, if anyone faced the problem and came up with a
> > solution, please share :)
>
> The EventBus is there to help you decouple things. If you want to
> "couple" them, then it's OK to not use the EventBus (you've been
> warned about strong coupling and its implication on code maintability
> though)
>
> You could follow the same pattern that is used in GWT widgets:
> - have a HandlerManager in ChildPresenter
> - ChildPresenter expose a addDeleteElementHandler method that adds
> the handler in the internal HandlerManager
> - the ParentPresenter registers its handler directly on the
> ChildPresenter
> - ChildPresenter fires the event at its internal HandlerManager
> rather than the EventBus
>
> This is really strong coupling though. You've been warned...
>
> Another solution would be to have several event buses.
>
> But actually I don't really understand why it is a problem to fire the
> event at the EventBus if you already have one...
>
> --
> 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]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>
--
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.