Activities & Places: How to listen to PlaceChangeEvents without an Activity?

295 views
Skip to first unread message

benneq

unread,
Oct 11, 2011, 4:27:53 PM10/11/11
to Google Web Toolkit
Hey guys,

I switched my GWT Project to Activities & Places. I have 2 panels:
- content
- menu

For the content I use Activities and it works pretty well. But for the
menu I don't want to use them, 'cause the menu is semi-static.
Somewhere I read about a similar situation and I think there was said,
that the menu doesn't need activities and it could listen to the
PlaceChangeEvents by itself. I looked around the GWT source code but I
couldn't find something like a PlaceChangeHandler...

Sure I could listen for HistoryChangeEvents but that's ugly, 'cause
I'd have another list of strings I'd have to manage. Instead it would
be far better if I could simply use my existing Places.


Thanks and good bye

Jens

unread,
Oct 11, 2011, 4:39:51 PM10/11/11
to google-we...@googlegroups.com
Hi,

PlaceChangeEvents are fired on the EventBus that you passed into your PlaceController. So you can listen to them via: 

eventBus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() {
  //implement method
})

-- J.

benneq

unread,
Oct 11, 2011, 5:49:52 PM10/11/11
to Google Web Toolkit
Thank you. Works pretty well :)

Is that GWT's new EventHandler structure? I mean: XyzEvent.Handler? No
more seperate Handler class?
And XyzEvent.TYPE instead of getType()? I like both changes pretty
much :)

Ashton Thomas

unread,
Oct 11, 2011, 6:16:15 PM10/11/11
to google-we...@googlegroups.com
I don't think this is really "new"

You can also define a CustomClass implements PlaceChangeEvent.Handler {}

then addHandler(PCE.TYPE, new CustomClass());

As for the .TYPE vs getType(), I do not know the details but if you look at PlaceChangeEvent extends GwtEvent it has the static field TYPE
but a dom event like ClickEvent still has getType()


Thomas Broyer

unread,
Oct 11, 2011, 6:36:55 PM10/11/11
to google-we...@googlegroups.com
DomEvent-s have their types lazily created, because they're registered in a global, er, registry, to easily match event types (from the browser, evt.type, i.e. a string such as "click", "mouseover", etc.) to DomEvent.Type instances.
Most other events don't need this, but some of them could benefit from it: some events have a static fire() helper method; when invoked, if the TYPE private field hasn't been initialized, it means getType() hasn't been called, i.e. no handler has been registered for the event, so the fire() help method can exit early, without event creating an event instance and calling the event bus.

Ashton Thomas

unread,
Oct 12, 2011, 1:06:19 AM10/12/11
to google-we...@googlegroups.com
Ah, that is helpful. Thanks, Thomas!
Reply all
Reply to author
Forward
0 new messages