0

Overview

  • There is a GWT method called: private void handleError()
  • There is a JSNI Javascript function called: private native void registerErrorHandler()
  • The native javascript function calls another function from a third party Javascript library: foo.attachEvent("EventName", handlerReference);

Functionality

I need to pass the GWT method as a function parameter into the foo.attachEvent() function, i tried several approaches:

foo.attachEvent("Error", registerErrorHandler); -> TypeMismatchException

foo.attachEvent("Error", [email protected]::registerErrorHandler()); -> TypeMismatchException

var handler = [email protected]::registerErrorHandler()();
foo.attachEvent("Error", handler);

-> TypeMismatchException

Plain Javascript

When I write this in plain Javascript, it's working:

function handleError() {
    alert("Error");
}
function registerErrorHandler() {
    var event = "Error";
    var handler = handleError;

    foo.attachEvent (event, handler);
}

How can I implement that into GWT? I am having a lot of problems completely understanding the Javascript - Java Objects conversion. I understand the handleError function reference as a JavaScriptObject, but I am not able to get it working with that information.

1
  • GWT must be executed within a RootLayoutPanel - This a complete page. You cannot mix Javascript with GWT generated functions. Purpose of GWT is not this. Commented Jul 11, 2012 at 7:58

1 Answer 1

0

you code needs to look like this: (there are only one time braces, therefore the function is not executed...)

var handler = [email protected]::registerErrorHandler();
foo.attachEvent("Error", handler);

but what you also want to do is wrap your function in the $entry function so that your uncaught exception handler will work:

var handler = [email protected]::registerErrorHandler();
foo.attachEvent("Error", $entry(handler));

What you can always do is build a js function that directly calls your gwt function:

var that = this;
var handler = function(){
    [email protected]::registerErrorHandler()();
};
foo.attachEvent("Error", $entry(handler));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your insight. Your first exampled worked instantly. However, the third party library is not attaching the event somehow, but when I define a new function like in your third example and provide an concrete implementation there (like $wnd.alert("handled!");) it's working. If i pass a GWT function with the special syntax, it's not working and somewhere in the gwt code (__gwt_tearOffGenerators)an exception is thrown. Do you have an idea for that one, too?
Ah now i understand the var that stuff. It's working. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.