• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Native method using JSNI

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using GWT and I have created a native method in 'MyClass' to define a Javascript function named myFunction

private native void myMethod(MyClass X) /*-{
$wnd['myFunction'] = function () {
X.@ex.samples.test.MyClass::javaMethod()();
};
}-*/;

This function 'myFunction' will be called onClick of a button and that calls the javaMethod in MyClass. This as such is working fine.

Now I want the name of the function that i define to be dynamic, i.e the name 'myFunction' should be set dynamically and the javascript function should be created with that dynamic name.

The purpose of this is, the code i have written is in a common class which will be used by many other classes. So when all the classes use the same code, there are methods created with the same name in the javascript and is not working properly.

Please suggest a good way to crack it.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure where to put this topic. Let's try the GWT forum.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Create a class which generates dynamic name on each call.

public class JSNIHelper {

private static int requestCounter = 0;

public static String registerCallbackFunction(MyClass obj) {

String dynamicName = "jsni" + (requestCounter++);
// you can write your own logic to generate dynamic name for

createDynamicFunction(obj, dynamicName);
return callbackName;
}

private native static void createDynamicFunction(MyClass obj, String dynamicName )/*-{
tmp = function( name ){
obj.@example.myclass::execute(Ljava/lang/String;)( name );
};
$wnd[dynamicName]=tmp;
}-*/;
}



// Your Class where you will call
// Pass object of 'MyClass'

public void onClick(ClickEvent event) {
String callbackName = JSNIHelper.registerCallbackFunction(new MyClass() {

});
// do other things
}

Hope it will help....
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic