jsni - Calling GWT Java function from JavaScript -


i newcomer gwt , javascript. there similar question of type have tried follow, keep failing.

i have gwt app, need call java function javascript( on onclick of href tag, in particular.) following have done.

public class jsnitest {   public static void handleanchorclick(int , int b) {    window.alert("current row , column " + + "  " + b);  }   public static native void exportmyfunction()/*-{     $wnd.handleanchorclick = function(param1,param2){         @company.package.class.jsnitest::handleanchorclick(*)(param1,param2);  }-*/;  } 

and in html,

<a href="javascript:handleanchorclick(a1,a2);">link</a>  

(a1 , a2) 2 integer variables in code. have called enclosingclass.exportmyfunction() in entry point function. keep running various kinds of exceptions(no such class exception). can please correct me?

regards

let me explain bit more exporting gwt stuff js world. have several options that, focus on 3 methods.

[edited]

0- jsinterop: gwt maintainers working in new feature export java methods javascript, , wrap javascript objects. feature experimental in 2.7.0 lacking features, in 2.8.0 functional. please take design document, , other discussions in mailing list.

[end]

1- jsni: first 1 write own jsni, in case have aware possible mistakes make. these mistakes because have know how deal types. in case if want javascript array (like asking in comment below), solution be:

public static native void exportmyfunction()/*-{   $wnd.handleanchorclick = @company.package.class.jsnitest::handleanchorclick(*); }-*/;  public static void handleanchorclick(jsarraymixed args) {   window.alert("current row , column " +                 args.getnumber(0) + "  " + args.getnumber(1)); }  public void onmoduleload() {   exportmyfunction(); }  //javascript code window.handleanchorclick([1,2]) 

note jsni allows pass primitive types (except long) , javascriptobject objects. when passing javascript array, have receive javascriptobject in example. in case, since javascript uses type numbers, args.getnumber return double, , have convert in java.

2- gwt-exporter exporting large projects, or when need handle complex objects , classes i'd rather use gwt-exporter

static class myclass implements exportable {   @export("$wnd.handleanchorclick")   public static void handleanchorclick(double[] args) {     window.alert("current row , column " +args[0] + "  " + args[1]);   } }  public void onmoduleload() {   gwt.create(myclass.class); }  //javascript code window.handleanchorclick([1,2]) 

gwt-exporter deal kind of primitive types (even long) myfunc(long[] args), var-args myfunc(long...args), supports method overload, , more.

3- gwtquery if prefer gwtquery, can use technique add function properties js object window

// gquery properties object able wrap java function object // js property. properties wnd = window.cast(); wnd.setfunction("handleanchorclick", new function() {   public void f() {     // js arguments[] array     jsarraymixed args = arguments(0);     // first element of arguments[] array     jsarraymixed ary = args.getobject(0);      window.alert("current row , column " +                   ary.getnumber(0) + "  " + ary.getnumber(1));   } });  //javascript code window.handleanchorclick([1,2]) 

with gquery can use gwt jsarraymixed class returns number double, or can use jscache allows convert numbers other numeric type in java ((jscache)ary.get(1, integer.class)

as summary, rather use gwt-exporter first option because specialized in handling problematic. second option, use gquery serious complement gwt. finally, avoid use hand-written jsni when possible, javascript source of issues , mistakes (think main goal of gwt not deal js).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -