Reload Place using getWhere()

390 views
Skip to first unread message

Mark Wengranowski

unread,
Nov 9, 2011, 5:19:18 PM11/9/11
to Google Web Toolkit
Hi Everyone,

Just wondering if anyone knows a way to reload the applications
current place in the following way:

PlaceController.goto(PlaceController.getWhere());

What happens is that the place controller realizes that it's the same
place your trying to go to so it doesn't do anything.

I could write an extremely long if statement that compares getWhere()
to all of my places but that seems a little long winded to do a simple
thing. ie:

if( (PlaceController.getWhere() instanceof TestPlace){
PlaceController.goto(new TestPlace());
}else if(.......

any simple workarounds would be greatly appreciated. I'm using the
activities and places mvp pattern.

Cheers,
-Mark

Jens

unread,
Nov 9, 2011, 6:34:46 PM11/9/11
to google-we...@googlegroups.com
1.) You can do a Window.Location.reload() which will reload your page which in turn will load the same place again (url does not change and should reflect the current app state).

2.) A different way would be to create a custom PlaceController that provides a method like .forcedGoTo(Place newPlace) which would skip the equals() check between getWhere() and the new place. PlaceController is a pretty simple class, take a look at its source code.

3.) Maybe you could also copy some code from the PlaceController if you dont want to introduce a custom PlaceController:

public class PlaceUtils {

  public static void reloadCurrentPlace(PlaceController.Delegate delegate, EventBus eventBus, PlaceController placeController) {
    Place where = placeController.getWhere();
    //Taken from PlaceController.maybeGoTo()
    PlaceChangeRequestEvent willChange = new PlaceChangeRequestEvent(where);
    eventBus.fire(willChange);
    String warning = willChange.getWarning();
    //Taken from PlaceController.goTo()
    if(warning == null || delegate.confirm(warning)) {
      eventBus.fire(new PlaceChangeEvent(where));
    }
  }

}

Haven't tried it but maybe it works. Thats basically what PlaceController.goTo() does but without the equals check (and this code also does not update the "where" instance variable of PlaceController as its not possible from outside. But as the code works on the same place instance the whole time it does not have to update the "where" variable). The default implementation of PlaceController.Delegate is PlaceController.DefaultDelegate.

-- J.

Mark Wengranowski

unread,
Nov 10, 2011, 2:24:06 PM11/10/11
to Google Web Toolkit
The code example you provided works perfectly! It reloads the place
without refreshing the entire page.

Instead of making a class i just added it into an onClick event in my
application. Because of this i couldn't figure out how to create the
delegate variable but removing it and the || delegate.confirm(warning)
didn't seem to affect it.

Thanks for your help.

Cheers,
-Mark

Jens

unread,
Nov 10, 2011, 5:46:21 PM11/10/11
to google-we...@googlegroups.com
Take a look at PlaceController.DefaultDelegate.confirm(). Its responsible for showing the warning message you may have returned in Activity.mayStop() via a confirm dialog (Window.confirm(warning)).

If your Activity.mayStop() always returns null then its ok to remove the confirm() thing. But if you return some message in Activity.mayStop() then you should add some code that shows the Window.confirm(warning) dialog again so the user can decide if he wants to reload the place of not.

-- J.

Thomas Broyer

unread,
Nov 10, 2011, 5:52:54 PM11/10/11
to google-we...@googlegroups.com
Hi Mark,

You shouldn't "abuse" places to tie them to data. If you need to refresh some data, you'd better dispatch an event on your event bus (an event that signals data has changed and need to be refreshed, not an event talking about navigation).

Tony B

unread,
Dec 1, 2014, 1:48:23 PM12/1/14
to google-we...@googlegroups.com
So how do I do that with events, then?  Do you have an example?
Reply all
Reply to author
Forward
0 new messages