Thursday, January 24, 2019

Re: Error When Calling java method via JSNI - Cannot read property 'apply' of undefined

I found the issue in my code.  The specific issue was how the parameters were defined in "handleJobDefinition".  It's basically a JSNI syntax error.  
$wnd.displayJobDefinitionDialog = $entry(@com.function.operation.Class::handleJobDefinition(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(jobId, jobType, serviceId));

--should be defined as --

$wnd.displayJobDefinitionDialog = $entry(@com.function.operation.Class::handleJobDefinition(III);

This JSNI reference was extremely helpful:  http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

On Wednesday, January 23, 2019 at 11:56:30 AM UTC-6, Thomas Broyer wrote:


On Tuesday, January 22, 2019 at 11:47:18 PM UTC+1, Jeff Ledgerwood wrote:
GWT 2.8.0


I am pulling my hair out with this error.  Hoping someone can help.  I am calling a Java method (in a GWT class) from an external javascript file.  I've done this without issue with several other java calls.  However, when I make the call from javascript to GWT and try to pass any parameters from javascript to the GWT method via JSNI, I get a cryptic error that I am unable to trace.


Javascript function  (used this same pattern on multiple occasions without issue)
function openJobDetailsDialog(){
this.displayJobDefinitionDialog("1", "2", "3");
}


GWT methods:

public native void displayJobDefinitionDialog(String jobId, String jobType, String serviceId) /*-{
$wnd.displayJobDefinitionDialog = $entry(@com.function.operation.Class::handleJobDefinition(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(jobId, jobType, serviceId));


This code calls handleJobDefinition and passes its returned value (which is 'undefined', given that the method's return type is 'void') to $entry(), which wraps it into a function.
So $wnd.displayJobDefinitionDialog is a function, but calling it will fail when $entry() will try to invoke the "wrapped" function (which is 'undefined').

I must say I don't understand your code: where are you expecting the 3 arguments to come from? Java or JS?

If they're supposed to come from Java, and JS would call the method with zero argument, then use:
$wnd.displayJobDefinitionDialog = $entry(function() { @Class:handleJobDefinition(*)(jobId, jobType, serviceId); });

and call it with
window.displayJobDefinitionDIalog();

otherwise, use:
$wnd.displayJobDefinitionDialog = $entry(@Class:handleJobDefinition(*));
or
$wnd.displayJobDefinitionDialog = $entry(function(a, b, c) { @Class:handleJobDefinition(*)(a, b, c) });


and call it as
window.displayJobDefinitionDialog("1", "2", "3");

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment