From javascript to java (GWT story)

So, I am developing an application in GWT for the embedded web browser (Sketchup). I can control Sketchup by changing the value of window.location to "skp :: myFunciton @myParams". Sketchup can execute javascript in a browser. What I want to do is ask sketchup to give me the contents of my model.

public static native void getModel() /*-{ $wnd.location = "skp: getModel@ "; }-*/; 

After the second sketch has a result. But how do we get back to gwt? The problem is that the entrypoint instance starts the request, and JSNI can only display static methods in javascript.

I thought I had a solution with events and elements ...

 //Sketchup javascript var gwtwidget = document.getElementById("myTextArea") gwtwidget.value = "blahblah"; gwtwidget.onchange(); 

and then listening to the change in the GWT. Alas, this does not work. Gwt's own event system redefines, absorbs, disables (or something else) the event. Which approach should I take? I was on the Internet looking for information, but I certainly can not get around it. I guess the answer is either ...

1 Call the entry point instance method from javascript (anyway)
2 Fire event from javascript to be raised by gwt (anyway)
3 Configure the asynchronous callback interface mechanism (somehow)

+3
source share
1 answer

The interface should be pretty simple.

For example, suppose we have some object in GWT. Let's say we have some function in JS that takes some callback as a parameter. So in gwt we will have something like this:

  public static native void executeFunctionWithCallBack(MyCallback callback)/*-{ var callBackWrapper =function(param) { callback.@com.package.MyObject ::onSuccess(*)(param); } $wnd.invokeFunctionWithCallback(callbackWrapper) }-*/; 

If you want to call instance methods, you need to set not only the method, but also the instance on which it must also be called. For instance. you need to pass the instance as a parameter to the JSNI method (or get it from JSNI in some other way). Then you create a JS function that will call the method on the instance. All this. No more magic =)

+6
source

Source: https://habr.com/ru/post/904404/


All Articles