Places - How to customize the place name? And what's the best way to use A&P with Gin?

38 views
Skip to first unread message

benneq

unread,
Oct 12, 2011, 4:09:27 PM10/12/11
to Google Web Toolkit
Hi,

I've now completely moved to Activities and Places. Works quite nice.
But now I want to make things look more pretty and a bit more
efficient.

Short about the website: It's a pretty simple Homepage. That means: We
have a menu (for changing places). And a content panel that switches
its activity when the place changes. Nothing special...

The first thing is: In my URL bar there's always the class name of the
place. I think it must be possible to use a custom name?! How can I do
this?

Second: Google says that we should make our Activities a Singleton,
'cause DOM Manipulation is bad ;) I've done that. My app still works
perferctly. Now I want to know if I could make Place a Singleton, too?
A place is a really small object, I know. But why should I recreate
it, if I don't have to?! So, are there any disadvantages saying Gin to
take my Place as Singleton?


(Third question: What do you think is the best way of dealing with
Activities and Places together with MVP? Actually I just moved my
Presenter Code into the Activity classes. Is there anything bad about
it?)

Thanks :)

Jens

unread,
Oct 12, 2011, 5:34:56 PM10/12/11
to google-we...@googlegroups.com
Hi,

1.) The url hash fragment (everything after "#") is by default defined as <prefix>:<token>. The prefix can be changed by using the @Prefix annotation on your PlaceTokenizers. If you do not use the @Prefix annotation the places class name will be used by default (thats what you currently have). Keep in mind that the names you provide with the help of the @Prefix annotation must be unique.

2.) You should make your views singleton as they are expensive to construct because of DOM operations. An activity is a pretty lightweight class you its fine if you recreate it. But of course you could make them also singleton.

3.) I think places can't be singletons but I am not quite sure about it. My first thought ist that PlaceController stores the current place and when you call PlaceController.goTo(newPlace) the new place will be compared to the current one using equals(). If they are equal nothing will happen.
If you have singletons and you switch between totally different places everything will be fine but if you want to switch between same places with different internal states you got a problem because they will always be equal. For each place you only have one singleton instance thus you only have one state. You can't have a current state and a new state for the same place instance (e.g. you are at PlaceA(null) and you want to switch to PlaceA(stateValue)).
Places are pretty small and easy classes, so just create new ones as you need them. 

-- J.

benneq

unread,
Oct 12, 2011, 5:47:10 PM10/12/11
to Google Web Toolkit
Thanks for your reply :)

Yeah sure, my Views are already Singleton (maybe someone could tell me
what's the difference using Gin @Singleton or .in(Singleton.class) ?!)
And my Presenters/Activities are Singleton, too.

After reading your thoughts, I'd say, that my Places could be
Singleton, too. 'Cause my menu doesn't allow switching from Place A to
Place A, but they're so small. I think I'll leave them as separate
Objects 'til someone says that it's safe in all cases.


Can you tell me 'how' to use these Place tokens? Are they meant to be
some kind of variables? (like x.html?var=value)? If that's true, I
hope that Google implemented some methods to easily safe and restore
variables and values...

Jens

unread,
Oct 12, 2011, 6:40:24 PM10/12/11
to google-we...@googlegroups.com
While the prefix identifies the place, the token stores the internal state of that place. 

How to convert a place state into a string token is up to the developer by implementing the getToken() method of PlaceTokenizer. If a MyPlace has two state variables s1 and s2 you could create tokens like #MyPlace:s1=1&s2=test or #MyPlace:s1:1,s2:test ... (its really up to you). With some more complex customization you can also achieve hash fragments like #/myplace/1/test which look more like a typical url (a good example would be the current URL in your browser as the new Google Groups website is done with GWT).

Well and the reverse way is also up to the developer. If someone visits your site via www.domain.com/#MyPlace:s1=1&s2=test you have to create a place based on the token (thats done in PlaceTokenizer.getPlace()). So you would split the token into its parts and construct a place "new MyPlace(1, test)". The Activity could then use the state information "1" and "test" to configure the view or to preselect something, etc.

So basically its somehow like URL parameters but its much more customizable as everything happens inside the hash fragment of the URL.

-- J.

benneq

unread,
Oct 13, 2011, 4:44:22 AM10/13/11
to Google Web Toolkit
Maybe the easiest way would be to use JSON ?! It will look a bit
strange in the URL bar (all those brackets), but should work pretty
well.
I think Google should provide a default way (using IsSerializable
Interface) to store and restore objects. What do you think?


On 13 Okt., 00:40, Jens <jens.nehlme...@gmail.com> wrote:
> While the prefix identifies the place, the token stores the internal state
> of that place.
>
> How to convert a place state into a string token is up to the developer by
> implementing the getToken() method of PlaceTokenizer. If a MyPlace has two
> state variables s1 and s2 you could create tokens like #MyPlace:s1=1&s2=test
> or #MyPlace:s1:1,s2:test ... (its really up to you). With some more complex
> customization you can also achieve hash fragments like #/myplace/1/test
> which look more like a typical url (a good example would be the current URL
> in your browser as the new Google Groups website is done with GWT).
>
> Well and the reverse way is also up to the developer. If someone visits your
> site viawww.domain.com/#MyPlace:s1=1&s2=testyou have to create a place
Reply all
Reply to author
Forward
0 new messages