Reputation: 1728
I have a requirement where I have to call a java GWT method from a javascript but it does not work. No exception is thrown but the application hangs somewhere and the code doesn't run. The application works fine when I don't make that call. My GWT-Compile is sucessful. I am testing in dev mode in Firefox.
My code is something like this -
class CPClass {
public native void jsCallViewDocument(String objectId)/*-{
$wnd.jsCallViewDocument =
$entry([email protected]::testJS(Ljava/lang/String;)(objectId));
}-*/;
private void testJS(String objectId) {
SC.say("Call successfull" + objectId);
}
private void setDocName(ListGridRecord newRecord, someDTO currDocInfo) {
String anchorTag = "<a href ='#' onclick = \"javascript:jsCallViewDocument('"
+ currDocInfo.getDocName() + "')\">" + currDocInfo.getDocName() + "</a>";
newRecord.setAttribute(Display.GRID_FIELD_DOC_NAME, anchorTag);
}
}
Till now I have used JSNI to make simple calls like $wnd.close() or $wnd.alert(), so I might be missing something. Does my class (where the Native method and the method to be called are defined) need to extend JavaScriptObject or does that native method to be registered first or something?
Upvotes: 3
Views: 2307
Reputation: 8671
I think you've got a bit confused here.
Your code as it is will only work if you have already invoked jsCallViewDocument()
from your Java code before the link is clicked -- otherwise the $wnd.jsCallViewDocument
function will not exist.
It would make no sense in that case to pass the string in to the function.
What you probably want instead is a function like this:
public static native void setupJsCallViewDocument(CPClass p) /*-{
var fn = function(objectId) {
// invoke our (Java) testJS function, passing objectId as a parameter
$entry(
[email protected]::testJS(Ljava/lang/String;)(objectId)
);
};
// assign this function to the global jsCallViewDocument
$wnd.jsCallViewDocument = fn;
}-*/;
Now in your setDocName
function:
private void setDocName(ListGridRecord newRecord, someDTO currDocInfo) {
// set up the native function call
setupJsCallViewDocument(this);
String anchorTag = "<a href ='#' onclick = \"javascript:jsCallViewDocument('"
+ currDocInfo.getDocName() + "')\">" + currDocInfo.getDocName() + "</a>";
newRecord.setAttribute(Display.GRID_FIELD_DOC_NAME, anchorTag);
}
Upvotes: 3
Reputation: 8510
You do not need javascript:
in onClick
attribute (as opposed to href
attribute)
switch to:
String anchorTag = "<a href ='#' onclick = \"jsCallViewDocument('"
+ currDocInfo.getDocName() + "')\">" + currDocInfo.getDocName() + "</a>";
or:
String anchorTag = "<a href = \"javascript:jsCallViewDocument('"
+ currDocInfo.getDocName() + "')\">" + currDocInfo.getDocName() + "</a>";
by the way why not assign event listener in java gwt code?
Upvotes: 0