开发者

Storing Javascript objects in Java

I have a Java object, and in it exists a spatial shape defined by a String of "well-known text". On mouseover of this object in my GWT UI, my code passes this String to Javascript through JSNI, which in turn does a bunch of parsing and creates appropriate Bing Maps API shapes.

A simplified example of this would be a map application where each state exists as an object containing the states name and a string defining its perimeter. On mouseover of the states name in my UI, the perimeter string is parsed and a representative shape drawn on the map.

So right now, my code does the passing, parsing, and creating every time the user mouses-over my object. I'm looking for a way to parse the strings and create the objects only once each, hopefully storing the full, already created Javascript objects (Bing Maps shapes) in the original Java objects themselves. This, I think, should surely speed things up - the UI starts to look laggy when it has to parse and create several very detailed map objects every time the user moves the mouse to a different item.

Is it possible to store Javascript objects of this nature in my Java objects, and then bring them back to Javascript whenever I need them?

In my Java code (GWT), I can create a JavascriptObject, bu开发者_JS百科t is this sufficient for holding something like a Microsoft.Maps.Polygon object?

Thanks!


Why not just cache them in javascript? Once you've created an object in the Javascript, store it in a cache object with the string as the key. Then, whenever you get a request for a new object, you check the cache and use the pre-created one if there is one. If there isn't a pre-created one, you create it and add it to the cache.

If you want to pre-create a number of objects, you could have the java just call the javascript with a bunch of strings for which it would pre-populate the cache with. This keeps all the Javascript objects on the Javascript side of the fence and all the Java objects on their site of the fence, yet should still help with the performance.

This is the general idea in pseudo-code:

var cache = {};

function createJSObject(stringArgument) {
    if (stringArgument in cache) {
        return(cache[stringArgument]);
    }
    var obj;

    // create the object here
    // ...

    // cache the object we created
    cache[stringArgument] = obj;
    return(obj);
}


You can very well have a field in your class, or a variable of type JavaScriptObject to store any object coming from JS.

GWT does just that in a few places already: for instance in com.google.gwt.xml.client.impl.DOMItem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜