custom event + event bubbling

57 views
Skip to first unread message

isern

unread,
Jun 24, 2011, 11:33:30 AM6/24/11
to Google Web Toolkit
Hello,

I was wondering, is it possible to bubble up a custom event through
the widget hierarchy?

The EventBus solution seems great but in some cases I'd like to catch
certain events related to the "current context" that's usually given
by the UI.

Thanks..!

Ashwin Desikan

unread,
Jun 25, 2011, 2:34:43 AM6/25/11
to google-we...@googlegroups.com
what sort of events are you looking to capture? You can create custom events and register those events with the eventBus. When ever that event occurs all handlers would receive a notification and you can take necessary action.

Thanks
Ashwin

isern

unread,
Jun 25, 2011, 7:10:29 PM6/25/11
to Google Web Toolkit
I come from the Flex/JS/Tapestry worlds and a technique commonly used
is to attach listeners to certain events expecting they'll will go up
like a bubble through the component hierarchy. Actually if I'm not
wrong this kind of behavior also occurs with native hardware events in
GWT.

A typical example is the Accept/Cancel widget. It's a widget because
it's repeated all over the application.

To reduce coupling to a minimum, I'd like to listen to the events
"accept" and "cancel" on the current widget (that contains accept/
cancel directly or indirectly), being aware that if users click
"accept" the event will be caught and what you do depends on that
context (UI structure)

The EventBus is okay, but it doesn't seem to take into account this
component hierarchy, all the event handlers are notified without
discrimination.

I've managed, however, to create a BubbledEvent/BubbledEventHandler
that only is dispatched if the current listener (a visual widget) is a
parent of that which triggered it.

I hope I was clear,

Best regards, Juan

ashwin....@gmail.com

unread,
Jun 26, 2011, 1:40:53 AM6/26/11
to google-we...@googlegroups.com
for listening on events on your widget you can define UiHandler or Attach / Add handlers

for example, if you have a button defined in your UIBinder

<g:Button ui:field = 'button1'>Hello</g:Button>

now if you want to listen on click event of this button, you can do either of these two operations

@UiHandler("button1")
public void OnButton1Click(ClickEvent event) {
 ///do what ever you want on button click
}

when you are not using a UiBinder you can define

button1.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent arg0) {
//do your opertaions
}
})


hope this is clear. 

Thanks
Ashwin

--
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-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.


isern

unread,
Jun 26, 2011, 6:17:49 PM6/26/11
to Google Web Toolkit
In the @UIHandler example, you have a direct reference to the button
and you register a handler directly on the event source.

I was wondering if there's the possibility that some custom event goes
up in the UI hierarchy.

To illustrate, in this situation I could perfectly bind a handler to
the widget that contains those buttons, and I still catch the click
event because of the principle of event bubbling, that's the default
behavior in JS DOM events.

Now my question was if I can use event bubbling with custom events
created by me.


On Jun 26, 2:40 am, "ashwin.desi...@gmail.com"
<ashwin.desi...@gmail.com> wrote:
> for listening on events on your widget you can define UiHandler or Attach /
> Add handlers
>
> for example, if you have a button defined in your UIBinder
>
> <g:Button ui:field = 'button1'>Hello</g:Button>
>
> now if you want to listen on click event of this button, you can do either
> of these two operations
>
> @UiHandler("button1")
> public void OnButton1Click(ClickEvent event) {
>  ///do what ever you want on button click
>
> }
>
> when you are not using a UiBinder you can define
>
> button1.addClickHandler(new ClickHandler() {
>  @Override
> public void onClick(ClickEvent arg0) {
> //do your opertaions
>
> }
> })
>
> hope this is clear.
>
> Thanks
> Ashwin
>

Ashwin Desikan

unread,
Jun 27, 2011, 4:39:24 AM6/27/11
to google-we...@googlegroups.com
You should be able to bubble custom events using the attachHandler and fireEvents method on a widget.

I am still trying to conceive an example where you would need a custom event. Most of the widely used events for all widgets have handlers defined.

I have not personally tried attaching a custom handlers to a widget but have created custom widgets by extending existing widgets like Cell where you can specify the events you want to listen on.

~ashwin

Sent from my iPhone

Reply all
Reply to author
Forward
0 new messages