In my project, I need to use some code written in an external JS. It looks like JSNI seems to be the solution to use. I am creating a test prototype right now to ensure both sides are working properly.
When I run and compile, I see the console logs in the browser from the events being called. However, the java methods are not being called.
After trying many scenarios, I also noticed that if I remove the $entry wrapper from the exportStaticMethods(), the opposite occurs. I see the System.out being called in my java code, however the console logs from the JavaScript in the browser are not being called.
I am trying to figure what is causing the behavior and if there is a small missing piece I overlooked or if there is anything SmartGWT specific maybe I need to do.
I have already reviewed the GWT JSNI documentation for calling a Java method from js.
GWT and Java side
I have gone into the onModuleLoad() method of my EntryPoint class and added a static method called exportStaticMethods(). I also created the PokePingClass.java file listed below.
EntryPointClass.java
public class EntryPointClass implements EntryPoint
{
@Override public void onModuleLoad()
{
exportStaticMethods();
// load application here.
}
public static native void exportStaticMethods() /*-{
$wnd.pingApp = $entry((function)
{ @com.application.PokePingClass::pingApp()(); });
}-*/ }
PokePingClass.java
public class PokePingClass
{
public static void pingApp()
{
System.out.println("pingApp() called");
}
}
HTML and js
In the .html file of the project, I added a hidden div element of id 'ping', as well as the ping.js file.
<html>
<head>
.
. <!-- other stuff -->
.
<script type='text/javascript' src='pokeping.js</script>
</head>
<body>
.
. <!-- other stuff -->
. <div id="ping" style="visibility: hidden;"></div>
</body>
</html>
ping.js
$(document).ready(function)
{
var $pp = $('#ping');
var pingApp = function()
{
console.log("app handling ping event");
window.parent.pingApp();
};
$pp.trigger('pingApp');
}
When I run and compile, I see the console logs in the browser from the events being called. However, the java methods are not being called.
After trying many scenarios, I also noticed that if I remove the $entry wrapper from the exportStaticMethods(), the opposite occurs. I see the System.out being called in my java code, however the console logs from the JavaScript in the browser are not being called.
I am trying to figure what is causing the behavior and if there is a small missing piece I overlooked or if there is anything SmartGWT specific maybe I need to do.
I have already reviewed the GWT JSNI documentation for calling a Java method from js.
GWT and Java side
I have gone into the onModuleLoad() method of my EntryPoint class and added a static method called exportStaticMethods(). I also created the PokePingClass.java file listed below.
EntryPointClass.java
public class EntryPointClass implements EntryPoint
{
@Override public void onModuleLoad()
{
exportStaticMethods();
// load application here.
}
public static native void exportStaticMethods() /*-{
$wnd.pingApp = $entry((function)
{ @com.application.PokePingClass::pingApp()(); });
}-*/ }
PokePingClass.java
public class PokePingClass
{
public static void pingApp()
{
System.out.println("pingApp() called");
}
}
HTML and js
In the .html file of the project, I added a hidden div element of id 'ping', as well as the ping.js file.
<html>
<head>
.
. <!-- other stuff -->
.
<script type='text/javascript' src='pokeping.js</script>
</head>
<body>
.
. <!-- other stuff -->
. <div id="ping" style="visibility: hidden;"></div>
</body>
</html>
ping.js
$(document).ready(function)
{
var $pp = $('#ping');
var pingApp = function()
{
console.log("app handling ping event");
window.parent.pingApp();
};
$pp.trigger('pingApp');
}
Comment