Tuesday, April 26, 2016

jsinterop: casting between jstypes

Hi,

I just started experimenting with the new jsinterop and I have one question.

I am using bootstrap which is using jquery, Although there are implementations around (https://github.com/workingflows/gwt-jquery) I thought to give it a try as a learning exercise, to see how far I can get before I incorporate somebody else's solution.

So here is the core from jquery

@JsType(isNative = true)
public class JQuery {
    @JsMethod(namespace = JsPackage.GLOBAL)
    public static native JQuerySelection $(String selector);
}

where JQuerySelection is
@JsType(isNative = true)
public class JQuerySelection {

}

and it works! Great

Now let's try to bring bootstrap inside. I want to user their modals. The way to get a modal is through jquery. Let's extend the jqueryselection

JsType(isNative = true)
public class BootstrapModal extends JQuerySelection {
    public native void modal(String command);

    @JsOverlay
    public final void show() {
        modal("show");
    }

    @JsOverlay
    public final void hide() {
        modal("hide");
    }
}

//////////////////// java caller  ----- It crashes on cast

((BootstrapModal) JQuery.$("#modal_id")).show();

Now when I try this from my java code it crashes (saying that  mypackage name (com.mypackage.) is not a property)

What is  the way to use jsinterop in order to capture the implicit parent/child relationship between BootstrapModal and JquerySelection?

I work around it by adding in BootstrapModal this
    @JsMethod(namespace = JsPackage.GLOBAL, name="$")
    public static native BootstrapModal getModal(String selector);

but it doesn't strike me as the correct way to do it.

Any hints?

--
Vassilis Virvilis

--
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