JsInterop Questions

386 views
Skip to first unread message

Julien Dramaix

unread,
Mar 31, 2015, 5:30:10 PM3/31/15
to google-web-tool...@googlegroups.com
Dear GWT lovers,

I've finally started to play with JsInterop in GWT 2.7 and mainly I'm trying to use JsInterop in order to write polymer components.

So far, I have two questions:

1. I'm using super dev mode and so I'm obliged to use the CrossSiteIframeLinker. As the javascript code generated by GWT is loaded in an iframe, the exported types are not accessible from the main window (only from the iframe). The workaround I found to expose java type from the main window is to prefix my namespace by $wnd
@JsNamespace("$wnd.jdramaix")
public class AgeSliderImpl
Isn't it a better way to do that ?

2. I want to expose some java field of my class to javascript field. I'm trying to use @JsProperty for this purpose but it seems not to work:

@JsNamespace("$wnd.jdramaix")
public class AgeSliderImpl implements AgeSlider {
public int age;

@Override
public int getAge() {
return age;
} }
@JsType
public interface AgeSlider {
@JsProperty
int getAge();
}
If I try to access the field in the javascript console,it is undefined:
> var slider = new jdramaix.AgeSliderImpl()
> slider.age
< undefined

But the method getAge exists:
> slider.getAge
< function getAge_0_g$(){
<   return this.age_3_g$;
< }
Same problem if I rename the method to age(). slider.age is defined by point to a function.

Does @JsProperty work (at least when we export Java to Javascript) in GWT 2.7. ? If so how to use it ?

Thanks

Julien

Goktug Gokdogan

unread,
Mar 31, 2015, 8:07:34 PM3/31/15
to google-web-toolkit-contributors
On Tue, Mar 31, 2015 at 2:30 PM, Julien Dramaix <julien....@gmail.com> wrote:
Dear GWT lovers,

I've finally started to play with JsInterop in GWT 2.7 and mainly I'm trying to use JsInterop in order to write polymer components.

So far, I have two questions:

1. I'm using super dev mode and so I'm obliged to use the CrossSiteIframeLinker. As the javascript code generated by GWT is loaded in an iframe, the exported types are not accessible from the main window (only from the iframe). The workaround I found to expose java type from the main window is to prefix my namespace by $wnd
@JsNamespace("$wnd.jdramaix")
public class AgeSliderImpl
Isn't it a better way to do that ?


Yes, in 2.7 you need to use $wnd to expose your code to main window. In GWT 2.8 snapshot, this is the default behavior, you don't need $wnd.
 
2. I want to expose some java field of my class to javascript field. I'm trying to use @JsProperty for this purpose but it seems not to work:

@JsNamespace("$wnd.jdramaix")
public class AgeSliderImpl implements AgeSlider {
public int age;

@Override
public int getAge() {
return age;
} }
@JsType
public interface AgeSlider {
@JsProperty
int getAge();
}
If I try to access the field in the javascript console,it is undefined:
> var slider = new jdramaix.AgeSliderImpl()
> slider.age
< undefined

But the method getAge exists:
> slider.getAge
< function getAge_0_g$(){
<   return this.age_3_g$;
< }
Same problem if I rename the method to age(). slider.age is defined by point to a function.

Does @JsProperty work (at least when we export Java to Javascript) in GWT 2.7. ? If so how to use it ?


@JsProperty methods defined in @JsType that is implemented by concrete classes does not work with GWT 2.7 (basically the scenario in your code snippet), it doesn't generate the code with a javascript property setter/getters.

In GWT 2.8 snapshot, javascript property setter/getter are still not implemented (yet) but instead you can use @JsType with concrete class and a public field, and you can skip the JsType interface completely.

i.e.
@JsType
public class AgeSlider {
public int age; }
if you want to export the constructor, don't forget to add @JsExport as well.

Thanks

Julien

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/CABb_3%3D6WZanScYy1ZqHBR23yssh9uvDLWe%3DquGaiixcpOusBog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Julien Dramaix

unread,
Apr 1, 2015, 8:27:55 AM4/1/15
to google-web-tool...@googlegroups.com
Thanks Goktug !

That works better with GWT 2.8 indeed.

However, it seems there is a bug with @JsExport and super dev mode in the last snapshot. When I use sdm, the prototype of my JsType is correctly defined (fields and function are defined with the right name) but the constructor is not exported and I cannot instantiated my Java object from Javascript.

AgeSlider.java:
@JsNamespace("jdramaix")

@JsType
public class AgeSlider {

public int age;

  @JsExport
public AgeSlider() {
} // other public methods are exported }
With sdm, in javascript, I'm not able to do: var slider = new jdramaix.AgeSlider();

By looking a bit in the javascript code generated by sdm, the part exporting the constructor under AgeSlider is not present:
_ = provide('jdramaix');
_.AgeSlider = AgeSlider;

That works fine with a normal compilation.

Julien


Goktug Gokdogan

unread,
Apr 1, 2015, 2:15:51 PM4/1/15
to google-web-toolkit-contributors, Ray Cromwell, John Stalcup
[+stalcup, +cromwellian]

If you have JsExport on a class in the first compilation, it works fine right? I guess, it doesn't work only if you add JsExport after the first compilation?

Last week we were suspecting exporting might not be working properly w/ SDM. We need to write some incremental compilation test to make sure this works properly before 2.8.


Julien Dramaix

unread,
Apr 1, 2015, 3:10:12 PM4/1/15
to google-web-toolkit-contributors, Ray Cromwell, John Stalcup

If you have JsExport on a class in the first compilation, it works fine right?
Nope I doesn't work. Even in the first compilation I receive a "Uncaught ReferenceError: jdramaix is not defined"

Julien Dramaix

unread,
Apr 2, 2015, 4:54:56 AM4/2/15
to google-web-toolkit-contributors, Ray Cromwell, John Stalcup
FYI, I've tested this morning with the last night's build and now it works fine with sdm.

Thanks

Julien

To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-contributors+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-contributors+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-contributors+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-contributors+unsubscribe@googlegroups.com.

Goktug Gokdogan

unread,
Apr 2, 2015, 2:07:20 PM4/2/15
to google-web-toolkit-contributors, Ray Cromwell, John Stalcup
Not sure but there is still a change that it might have just worked because SDK version change invalidated the caches.
Does it work fine when you add a JsExport after the first compile?

To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/CABb_3%3D5Vc8ie%3D0FLghOP4TpvK83rBh4ZFZ2D80A07Nd%3Du4Bf6w%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages