HashMap to JavaScriptObject

455 views
Skip to first unread message

Riyaz Mansoor

unread,
Dec 23, 2007, 9:08:07 AM12/23/07
to Google Web Toolkit

Hi

I need to pass all the key/value pairs in a Map to JavaScriptObject

for each key in Map
JSO[key] = value

How can I do this?

Riyaz

Riyaz Mansoor

unread,
Dec 23, 2007, 11:58:01 AM12/23/07
to Google Web Toolkit

In case I was not clear,

for each key in Map
JSO[key] = value <== JSO[key] here refers the Java Script object
represented by JSO

Reinier Zwitserloot

unread,
Dec 23, 2007, 8:09:21 PM12/23/07
to Google Web Toolkit
Some combo of entrySet(), Iterator, and JSNI.

Riyaz Mansoor

unread,
Dec 23, 2007, 9:58:37 PM12/23/07
to Google Web Toolkit

I was hoping for a non JSNI solution. I've looked at the source code
for DOM.setElementProperty and this seems promising. Haven't tried it
yet though.

Riyaz

Riyaz Mansoor

unread,
Dec 24, 2007, 2:46:30 PM12/24/07
to Google Web Toolkit

DOM.setElementProperty does not seem to work.

public static native void do(JavaScriptObject jsoFrom,
JavaScriptObject jsoTo) /*-{
for(var x in jsoFrom) {
jsoToj[x] = jsoFrom[x];
}
}-*/;

I have a new implementation; jsoFrom as a GWT HashMap, jsoTo is
myWidget that extends GWT Widget. How can I transfer the Map contents
to myWidget, using java for the same results as the above native
javascript code ?

Riyaz

Reinier Zwitserloot

unread,
Dec 24, 2007, 4:00:21 PM12/24/07
to Google Web Toolkit
JavaScriptObject does absolutely nothing java-side. It exists merely
to pass an object between JSNI and java land and keep track of it. To
do -anything-, other than pass it around, to a JSO, you NEED to go
into JSNI.

You can't load a hashmap into a Widget; Widget doesn't have the
classes. If you want to dip into JSNI land and add the key/value pairs
of hashmap onto the widget object, that will probably work, but it's
an ugly, -ugly- hack, and you can break something at ANY time - gwt
assumes you don't mess with the namespace on any of the GWT created
objects. Randomly, you might assign/edit/read a key which GWT randomly
picked during obfuscation (which reduces all identifiers to much
shorter unreadable strings to save space). One compilation run
everything works, you change one byte in a completely different place,
and now all of a sudden you have -really- obscure bugs.

This has got 'bad idea' written all over it. What's wrong with JSNI
again?

Riyaz Mansoor

unread,
Dec 24, 2007, 10:52:38 PM12/24/07
to Google Web Toolkit

Hmmm. Man. Am I confused! My background in javascript is lacking. Back
to basics.

Are these JavaScriptObject jso (essentially) identical?
= JavaScriptObject.createObject()
= (native method call) return new Object();

Now to this *javascript* object (represented by jso) I want to add a
property (and the result should be like below)
myJavascriptO['prop']='value';

I *thought* this was a basic operation and GWT would support that
without me writing javascript code in a native method. Are my thinking
on the wrong track?

Riyaz

Reinier Zwitserloot

unread,
Dec 26, 2007, 8:15:09 PM12/26/07
to Google Web Toolkit
Yup, wrong track.

they are NOT the same calls. One creates an empty javascript object,
and is functionally completely equivalent to:

var jso = {};

in javascript. GWT can NOT interact AT ALL with javascript stuff, save
some obscure low-level stuff in the DOM static utility class, and, of
course, JSNI. Therefore, JavaScriptObject.createObject(); is useless
from a GWT perspective; it might have a little use if you're
integrating with legacy javascript or you want to use deeply
interwoven non-GWT JS libraries.

new Object(); does something entirely different; it creates a java
object. GWT of course emulates this java object with a javascript
object; it has to compile down to the JS platform, after all. However,
this particular JSO is filled with a bunch of extra functions and
attributes (including a string that contains 'java.lang.Object', and
probably some stuff for a more java-esque hashCode, toString, and
equals). Because of obfuscation, you already can't use even 'new
Object' as a javascript entity with a publically mutable namespace.

Let's put this somewhat differently:

There *IS NO* GWT equivalent to 'myJavascriptO['prop'] = value' - 'all
objects are hashmaps' is a programming paradigm used by javascript,
ruby, python, and other languages, but NOT by java. And therefore,
not by GWT. If you need to do this thing, you'll need to either A)
dip into JSNI or B) use a simplistic wrapper method, which might even
exist in the GWT core libraries, but if it does, it will be in 'low
level glue' like e.g. DOM.*, and you're not supposed to use it in an
actual app unless you know what you're doing (read: know a LOT of
javascript, css, html, and the specifics of how GWT works internally),
or you'll cause obscure bugs, memory leakage, and other Bad Things.

Perhaps you should explain what you're trying to do on a lower level,
because if you think the solution is "I'll toss some undeclared
properties into a GWT object", you came up with the wrong solution.
That's the right answer in many languages, but not in java.
Reply all
Reply to author
Forward
0 new messages