Thursday, December 19, 2013

Need help with manipulating java originated objects in javascript

My goal is to initiate RPC calls directly from javascript.  I have come up with ways to fake callbacks (because of the asynchronous nature of RPC) but I can't figure out how to get custom objects into javascript.  

So, I've created a class named Interop and I statically create the service I'm interested in (had to use static as it was all I could get working, I don't think it's relevant right now):

    public class Interop {
        private static final GreetingServiceAsync service = GWT.create(GreetingService.class);
        ...
    }
    
I then create a function that will do the async calls and handle the responses:

    public static void greetServer(final String success, final String failure) {
    service.greetServer(
    "Homer", 
    new AsyncCallback<String>() {
    public void onFailure(Throwable caught) {
    callback(failure, caught.toString());

    }
    public void onSuccess(String result) {
    callback(success, result);
    }
    }
    );
    }

Then I create a JSNI function to export this function to javascript which I call from the onModuleLoad():

    public static native void export() /*-{
$wnd.greetServer = $entry(@edu.washington.apl.imp.client.Interop::greetServer(Ljava/lang/String;Ljava/lang/String;));
    }-*/;

And also create another JSNI function to deal with the callbacks:

    public static native void callback(String func, String response) /*-{
    $wnd[func](response);
    }-*/;

So that the function names I pass into greetServer() initially for success and failure are called by the JSNI as callbacks.  And this all works great when dealing with Strings or (I assume) a primitive type).  But when I try to do this with custom types (note the altered Custom type parameter):

    public static native void callback(String func, Custom response) /*-{
    $wnd[func](response);
    }-*/;

Then what ends up in javascript doesn't seem to work.  It seems to be a javascript object with cascading arrays and none of the methods are available.

So, the question is, how can Java-originated objects that aren't basic or primitives be accessed from within javascript (not JSNI)?  From what I can tell JavaScriptObject needs to originate in javascript, but in my case, my objects are originating in Java.  What can I do?

I've also looked into gwt-exporter and that shows how to instantiate java stuff from javascript, but not how to access java-originated stuff in javascript.

I know this is a bit confusing so please let me know if you have any questions.  Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" 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 http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment