Wednesday, February 8, 2017

List of native JsType

Hi

I am trying to wrap a javascript library using JsInterop. I do have a problem when using lists of native JsTypes.

I have a very simple js file:
function createMyObject()
{
return {value:10};
}

And a JsType:
@JsType(isNative = true) 
public class InteropTest {
public int value;
@JsMethod(namespace =JsPackage.GLOBAL)
public static native InteropTest createMyObject();
}

When I use this construct like this:
public void onModuleLoad() {
List<InteropTest> list = new ArrayList<InteropTest>();
for (int i = 0; i < 10; i++) {
InteropTest t = InteropTest.createMyObject();
list.add(t);
}
list.forEach(t -> GWT.log("Value "+t.value));
    }

It works fine. But when I use it like this:
public void onModuleLoad() {
List<InteropTest> list = new ArrayList<InteropTest>();
for (int i = 0; i < 10; i++) {
InteropTest t = InteropTest.createMyObject();
list.add(t);
}
for(InteropTest t:list)
{
GWT.log("Value "+t.value);
}
}

I get a JavaScript error "Cannot read property 'test' of undefined". Where test is my package name.

Do you know why this happens? Am I doing something wrong?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment