在 Java 中存储 Javascript 对象
我有一个 Java 对象,其中存在由“众所周知的文本”字符串定义的空间形状。当鼠标悬停在我的 GWT UI 中的该对象上时,我的代码通过 JSNI 将此字符串传递给 Javascript,JSNI 反过来进行一系列解析并创建适当的 Bing Maps API 形状。
一个简化的示例是地图应用程序,其中每个州都作为一个对象存在,其中包含州名称和定义其周界的字符串。将鼠标悬停在我的 UI 中的州名称上时,将解析周边字符串并在地图上绘制代表性形状。
所以现在,每次用户将鼠标悬停在我的对象上时,我的代码都会进行传递、解析和创建。我正在寻找一种方法来解析字符串并仅创建一次对象,希望将完整的、已创建的 Javascript 对象(Bing 地图形状)存储在原始 Java 对象本身中。我认为,这肯定会加快速度 - 当用户每次将鼠标移动到不同的项目时,它必须解析和创建几个非常详细的地图对象时,UI 就会开始显得滞后。
是否可以将这种性质的 Javascript 对象存储在我的 Java 对象中,然后在需要时将它们带回 Javascript?
在我的 Java 代码 (GWT) 中,我可以创建一个 JavascriptObject,但这足以容纳 Microsoft.Maps.Polygon 对象之类的东西吗?
谢谢!
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, but is this sufficient for holding something like a Microsoft.Maps.Polygon object?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接将它们缓存在 JavaScript 中呢?在 Javascript 中创建对象后,将其存储在缓存对象中,并以字符串作为键。然后,每当您收到对新对象的请求时,您都会检查缓存并使用预先创建的对象(如果有)。如果没有预先创建的,则创建它并将其添加到缓存中。
如果您想预先创建多个对象,您可以让 java 只需使用一堆字符串调用 javascript,以便预先填充缓存。这将所有 Javascript 对象保留在栅栏的 Javascript 一侧,并将所有 Java 对象保留在其栅栏站点上,但仍有助于提高性能。
这是伪代码的一般思想:
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:
您可以在类中使用一个字段,或者一个
JavaScriptObject
类型的变量来存储来自 JS 的任何对象。GWT 已经在一些地方做到了这一点:例如在
com.google.gwt.xml.client.impl.DOMItem
中。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
.