Hi Sylvain,
If the new window that you want to open will essentially be another instance
of your application loaded in some initial state based on the data your user
wants to view, here's a possible solution. You could use the History API to
retrieve a URL token which can tell your application which state it's in,
while initially loading in a blank state (i.e. no #urltoken).
For example, the code snippet below demonstrates a GWT module that reads in
the URL token to figure out what to display in the label widget:
public void onModuleLoad() {
VerticalPanel mainPanel = new VerticalPanel();
Label label = new Label("Beef");
final String token = History.getToken();
if (!"".equals(token)) {
loadState(label, token);
}
Button chicken = new Button("Chicken");
chicken.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (token != null && !"".equals(token)) {
Window.open("../Test.html#Chicken", "_blank", "");
} else {
openWindowFromFrame("../Test.html#Chicken", "_blank", "");
}
}
});
Button beef = new Button("Beef");
beef.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (token != null && !"".equals(token)) {
Window.open("../Test.html#Beef", "_blank", "");
} else {
openWindowFromFrame("../Test.html#Beef", "_blank", "");
}
}
});
mainPanel.add(label);
mainPanel.add(chicken);
mainPanel.add(beef);
RootPanel.get().add(mainPanel);
}
private void loadState(Label label, String token) {
label.setText(token);
}
private native void openWindowFromFrame(String url, String name,
String features) /*-{
window.open(url, name, features);
}-*/;
A few thing to note about this approach and the example code snippet:
1) This is a very naive example. In an actual application, you would need
some validation logic on the URL token as well as take care of other
components that might need to be aware of the state of the application when
loading up.
2) This will incur an extra roundtrip for the initial .nocache.js bootstrap
file. On the other hand, the specific .cache.html version of the application
should be cached (provided you set the cache headers when serving the
.cache.html files from your web server), so you won't have to pay a penalty
for fetching the actual application itself for the client browser / locale.
3) You will need to replace calls to any Window.someMethod() calls by JSNI
method equivalents that use the window object in native JavaScript whenever
the application is running from one of the newly opened windows. The reason
for this is because GWT applications originally load up in their own iframes
and bind to the window object of the containing parent window through the
$wnd object. In the case that the application is loaded in an separate
window, the $wnd variable no longer refers to the same window, so you'll
need to refer to it via JSNI methods through the standard window object.
You may want to consider another approach if the view loaded in the new
window actually isn't another instance of your application. If the view is
lightweight enough, you could setup a regular HTML page as a view to the
data you want to display and pass URL parameters that you can parse and use
to fill in the various data elements in your HTML page. You may even
consider creating a second GWT module if the view in the second window is
complex enough and radically different from your initial application, but
then your users would pay the cost of waiting on two roundtrips for the
second GWT module (one for the .nocache.js and one for the .cache.html, plus
an extra roundtrip for every additional resource).
In such a case you're probably better off using the History approach since
you can take advantage of point 2) above, and you also get smaller generated
JavaScript size overall since the compiler will be able to perform a
monolithic compile.
Hope that helps,
-Sumit Chandel
On Tue, Jun 2, 2009 at 3:26 AM, Sylvain <[email protected]> wrote:
>
> Hi Everybody,
>
> Could somebody be nice enough to post snippets of code around handling
> multiple windows in a GWT app. I've read the bug #404 entry and the
> pieces of suggested workarounds with window.open() but somehow I'm not
> connecting the dots.
>
> What I'm trying to achieve is:
> - Main window shows list of items in a table
> - User selects an item
> - User clicks on a 'Show Details' button
> - A new window pops up with the details of the item selected in the
> parent window
>
> Because of the nature of the information to be displayed and the fact
> that several detailed views may be opened at once I am not interested
> in using internal dialogs, panels etc.
>
> Thanks in advance.
>
> Regards,
> Sylvain
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---