1

I am trying to implement an API (SCORM API) using GWT. The client code expects an API object with methods like Initialize(), getLastError() and so on...

I tried to implement this api as an Java Object, but i see that the compiled names are changed and cannot be used directly by client code.

I see that gwt-exporter can do the trick (http://code.google.com/p/gwt-exporter/) but i would like to know how to do it using pure gwt and jsni.

As the API is expected as a object, named API_1484_11 attached to the window object, not an function, , i don't see how to use the $entry() idiom.

Here is my current, failing, code:

public final class SCORMApi {

    protected SCORMApi() {}

    public void Initialize(){
        GWT.log("** INITIALIZE CALLED **");
    }

    public static void create(){
        bind(new SCORMApi());
    }

    public static native void bind(SCORMApi api) /*-{
        $wnd.API_1484_11 = api;
    }-*/;

}

So, in this context, my question is:

How can i get javascript calls (e.g. window.API_1484_11.Initialize() ) to reach my java gwt code?

1 Answer 1

3

You're on the right lines with your bind method. But you haven't understood how to call Java methods from within JSNI. This is how you do it in the case of your Initialize method:

public static native void bind(SCORMApi api) /*-{
    $wnd.API_1484_11 = {
        initialize: function() {
            $entry( [email protected]::Initialize()() );
        }
    };
}-*/;

The blogs Getting To Really Know GWT parts 1 and 2 are required reading on this subject.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that is good to know. I am actually using something like this but was trying not to write the api twice (once in the javascript inline object and once in java). But i guess that is as good as it gets...
On my code i have not used the $entry, as did the part 1 of the article you mentioned ("Creating JavaScript libraries with GWT" part), and it seems to work all right. The docs are quite vague on the workings of $entry, would you know where to find more about it?
Do consider using GWT Exporter, I use it extensively exactly for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.