Michael Wiles
Michael Wiles

Reputation: 21186

Why is my @JsProperty not preserving the name in javascript?

I have the following class:

@JsType
public class Options {

    @JsProperty
    public boolean extractUrlsWithoutProtocol;

    public Options(boolean extractUrlsWithoutProtocol) {
        this.extractUrlsWithoutProtocol = extractUrlsWithoutProtocol;
    }
}

Now I pass it into a javascript method and when I use developer tools to inspect I get that property name is extractUrlsWithoutProtocol_0_g$

What's more if I remove the @JsProperty annotation I get no change to the generated code...

Update: What does work is

   public native void setExtractUrlsWithoutProtocol(boolean extractUrlsWIthoutProtocol_)
/*-{
    this.extractUrlsWithoutProtocol = extractUrlsWIthoutProtocol_;
}-*/;

Upvotes: 1

Views: 451

Answers (2)

googol4u
googol4u

Reputation: 91

The class should be annotated as a javascript object.

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

    @JsType(isNative=true,namespace=JsPackage.GLOBAL,name="Object")
public class Options {

    @JsProperty
    public boolean extractUrlsWithoutProtocol;

    public Options(boolean extractUrlsWithoutProtocol) {
        this.extractUrlsWithoutProtocol = extractUrlsWithoutProtocol;
    }
}

Upvotes: 0

Andrei
Andrei

Reputation: 1635

Edit: I assume you're talking about GWT 2.8. If other, my answer doesn't apply.

I think you're missing a @JsType annotation on the class (not sure about this, but I think the GWT compiler might be ignoring types not annotated with @JsType, even though you do have @JsProperty). Also, if your problem is ONLY when compiling in production mode, please be aware that you need a special compiler flag - generateJsInteropExports (the default is NOT to honor JS Interop annotations).

Upvotes: 2

Related Questions