I'm trying to get a native JS bridge working. For those of you who
don't understand this is providing native JS constructors/functions to
your GWT objects/classes. When trying to access these functions from
js it always says that "Employee is not defined" in the example
below...
Obviously the export() has either not been executed, or there is
something wrong with my JSNI code.
----------------------------------------------------
package jsni.export.test;
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Employee createEmployee(String name) {
return new Employee(name);
}
public native void export() /*-{
$wnd.Employee = function(name) {
// call factory method and store GWT reference
this.instance = @jsni.export.test.Employee::createEmployee(Ljava/
lang/String;)(name);
}
var _= $wnd.Employee.prototype;
_.getNameName = function() {
this.instance.@jsni.export.test.Employee::getName()();
}
}-*/;
}
--------------------------------------
js.....
var employee = new Employee('James Bond');
alert('Welcome '+employee.getName());
---------------
Can anyone confirm:
+ My JSNI/Code above looks ok... particularly..
this.instance = @jsni.export.test.Employee::createEmployee(Ljava/lang/
String;)(name);
+ Do I need to exclusively call Employee.export() from somewhere to
get method this to run? I have tried this, but it still doesn't seem
to matter.
+ Is there a problem with this not being in the *.client.Employee
package? I tried both inside and out without success.
thanks y`all
Try to call it from a button, you may have more success.
is there a way around this... onLoad() could be used on the page?
On Oct 30, 7:10 pm, "David Clément" <dav.clem...@gmail.com> wrote:
> You cannot call it directly in JS, GWT is not loaded when this code is running.
>
> Try to call it from a button, you may have more success.
>
> > this.instan...@jsni.export.test.Employee::getName()();
Another option is to add a JSNI method call at the end of your GWT onModuleLoad.
As example a JSNI method like
if($wnd.onGWTLoaded){
$wnd.onGWTLoaded();
}
On 10/30/07, ahhughes <ahhu...@gmail.com> wrote:
>
var gwtApplication = false;
while(!gwtApplication){
try {
gwtApplication = exportedGWTFunction();
} catch (e){
//pause for a micro second or so...
On 10/30/07, ahhughes <ahhu...@gmail.com> wrote:
>
or did I miss something?
public native void export() /*-{
$wnd.Employee = function(name) {
// call factory method and store GWT reference
this.instance =
@jsni.export.test.Employee::createEmployee(Ljava/
lang/String;)(name);
}
var _= $wnd.Employee.prototype;
_.getNameName = function() {
this.instan...@jsni.export.test.Employee::getName()();
}
//call the known function the js programmer must
provide
$wnd.initEmployee();
}-*/;
On 10/30/07, ahhughes <ahhu...@gmail.com> wrote:
>
On Oct 30, 11:34 pm, "David Clément" <dav.clem...@gmail.com> wrote:
> Yes, that's what you need to do unless these methods are called once
> GWT is loaded (from user interaction, as example)
>
I've this but I just get employee.getName() being 'undefined'....
----HTML/JS----
<html>
......
<button onclick="runJSNI()">Run JSNI</button>
<script type="text/javascript">
runJSNI = function(){
var employee = new Employee('James Bond');
alert('The new employee is: '+employee.getName());
}
</script>
....
</html>
----Export of GWT via JSNI----
package mygwtapp.client;
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Employee createEmployee(String name) {
return new Employee(name);
}
public native void export() /*-{
$wnd.Employee = function(name) {
// call factory method and store GWT reference
this.instance =
@mygwtapp.client.Employee::createEmployee(Ljava/lang/String)(name);
}
var _= $wnd.Employee.prototype;
_.getName = function() {
this.instance.@mygwtappclient.Employee::getName()
();
}
}-*/;
}
----Entry Point that triggers the Empoyee JSNI export()----
package myelders.demand.broadband.client;
import mygwtapp.client.contribute.ContributeWidget;
import com.google.gwt.core.client.EntryPoint;
public class Application implements EntryPoint {
public void onModuleLoad() {
Employee employee = new Employee("BLAH");
employee.export();
}
}
------
My head hurts :'( debugging native JS, calling JSNI, from GWT driven
JS...