Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created August 5, 2016 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/3c647b8e7db15d2f6a9bcb35948132b3 to your computer and use it in GitHub Desktop.
Save branflake2267/3c647b8e7db15d2f6a9bcb35948132b3 to your computer and use it in GitHub Desktop.
Here is a simple GWT 2.8.0-RC1 JsInterop example showing how to use a JsOverlay and JsProperty to provide access to a String literal array written in javascript.
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>GXT Project 4.0</title>
<script type="text/javascript" src="MyGxtProject40/MyGxtProject40.nocache.js"></script>
<script>
var myobject = ['a', 'b', 'c'];
</script>
</head>
<body>
</body>
</html>
import com.google.gwt.core.shared.GWT;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;
/**
* <script>
* var myobject = ["a", "b", "c"];
* </script>
*/
@JsType(isNative = true)
public class MyHelper {
@JsProperty(namespace = JsPackage.GLOBAL, name = "myobject")
private static String[] myobject;
@JsOverlay
public static String getItem(int index) {
//GWT.debugger();
return myobject[index];
}
@JsOverlay
public static void setItem(int index, String value) {
myobject[index] = value;
}
}
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.info.Info;
public class MyHelperTest implements EntryPoint {
@Override
public void onModuleLoad() {
TextButton button = new TextButton("Test getting");
button.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
test();
}
});
RootPanel.get().add(button);
}
protected void test() {
String value = MyHelper.getItem(1);
Info.display("work?", "value=" + value);
MyHelper.setItem(1, "changed");
value = MyHelper.getItem(1);
Info.display("work?", "value=" + value);
}
}
@saadhamouine
Copy link

Hello, thank for the nice and simple tutorial. I have a question though.
How do you reference the JS library in your code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment