tags:

views:

1251

answers:

3

I have been attempting to do the following for a few days to no avail:

I have a GWT application that makes several RPC calls on startup to load initial data. For the most part, this data is relatively static, so I'd skip the RPC call if I could. But the data changes more often than the application does, so I'd rather not include it at compile time.

Instead, I take the data and store it in JSON in a static .js file. I include that in the loading HTML for the application:

html ... GWT loading stuff...>

script src="staticstuff.js">/script> //this is intentionally wrong so it will show on stackoverflow html with static stuff looking like:

var startupdata = [JSON.....];

Then, in the application itself, we do the following:

private native JsArrayString getStaticData()/*-{
 return $wnd.startupdata;
}-*/;

This works perfectly fine when our data is an array, or string, or some other JS native object. What I cannot seem to do is the following:

private native OurMoreComplexObject getStaticData()/*-{
 return $wnd.startupdata;
}-*/;

I could pull back the js objects and parse them into my object graph myself, but I'd prefer not to, especially since GWT already created a converter for me (since OurMoreComplexObject) is already returned in RPC calls.

Has anyone attempted something like this before? If so, how do I get it to work? Is there some other way to do this? (The main goal is that if the data is stored in a file, I can load it onto our CDN instead of forcing the call back to our servers at startup every time)

+1  A: 

Yes (this is something I do a lot):

http://wiki.shiftyjelly.com/index.php/GWT#Speed_up_Page_Loading.2C_by_pre-serializing_your_GWT_calls

The basics of it are that you make the call when your server is rendering the page, and get GWT to serialize it to it's normal RPC form, and store it in the page in a javascript variable. From there you can read it in. The link above should hopefully have enough detail for you.

The main benefit being that your client gets the page all in HTML/js and doesn't need to make an AJAX call to get more data.

rustyshelf
and to answer the OP's question, you can just intercept the RPC call, and cache the response to a file statically, and on the next call, instead of allowing the rpc call to go through, you can just return what was cached.
Chii
Just a note to people trying to implement this. If you're stuck with GWT 1.4 (like I am), you can't get ahold of GWT's RPC internals on the client side (http://code.google.com/p/google-web-toolkit/issues/detail?id=460). You'll have to do it manually with JSON
Steve Armstrong
A: 

There's a good article on the same topic at: http://www.techhui.com/profiles/blogs/simpler-and-speedier-gwt-with

A: 

If you want to stick with JSON (and not serialize an RPC response) than you can use the JavaScriptObject wrapper a.k.a. JSO. This is a great way to get complex object graphs from JSON w/o having to write a bunch of marshaling code. It follows this pattern:

startupData = { "someProperty" : "someValue", "someList" : [ { "more", "json" } ] };


public class OurMoreComplexObject extends JavaScriptObject {
    protected OurMoreComplexObject() {}

    public native String getSomeProperty() /*-{
        return this.someProperty;
    }-*/;

    public native JSArray getSomeList()  /*-{
        return this.someList;
    }-*/;
}

The good news is that this has semi-magical properties in that your 'getStaticData' method will just work. The bad news is that you can't treat those JSOs as easily as POJOs. Dealing with list iteration (via JSArray) can be a particular pain.

You can check out more here.