如何将GWT方法作为参数传递给Javascript函数? [英] How to pass a GWT method as a parameter into a Javascript function?

查看:123
本文介绍了如何将GWT方法作为参数传递给Javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述




  • 有一个名为 private void handleError )

  • 有一个JSNI Javascript函数调用: private native void registerErrorHandler()

  • 原生javascript函数调用第三方Javascript库中的另一个函数: foo.attachEvent(EventName,handlerReference);



  • 功能

    我需要将GWT方法作为函数参数放到 foo.attachEvent()函数中,我尝试了几种方法:

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

    foo.attachEvent Error,this。@ package.foo :: registerErrorHandler()); - >
    TypeMismatchException

      var handler = this。@ package.foo :: registerErrorHandler()(); 
    foo.attachEvent(Error,handler);

    - >
    TypeMismatchException



    纯Javascript>



    当我用纯JavaScript写这篇文章时,它正在工作:

     函数handleError(){
    alert(Error);
    }
    函数registerErrorHandler(){
    var event =Error;
    var handler = handleError;

    foo.attachEvent(event,handler);
    }

    如何将其实现到GWT中?我有很多问题完全理解Javascript - Java对象转换。我将 handleError 函数引用理解为 JavaScriptObject ,但我无法使用该信息来处理它。 / p> 解析方案

您的代码需要如下所示:(只有一个时间大括号,因此函数不会执行...)

  var handler = this。@ package.foo :: registerErrorHandler(); 
foo.attachEvent(Error,handler);

但是你也想要做的是将你的函数封装在$ entry函数中,这样你的未捕获的异常处理程序将起作用:

  var handler = this。@ package.foo :: registerErrorHandler(); 
foo.attachEvent(Error,$ entry(handler));

你总是可以做的是构建一个直接调用你的gwt函数的js函数:

  var that = this; 
var handler = function(){
。@ package.foo :: registerErrorHandler()();
};
foo.attachEvent(Error,$ entry(handler));


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", this.@package.foo::registerErrorHandler()); -> TypeMismatchException

var handler = this.@package.foo::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.

解决方案

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

var handler = this.@package.foo::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 = this.@package.foo::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(){
    that.@package.foo::registerErrorHandler()();
};
foo.attachEvent("Error", $entry(handler));

这篇关于如何将GWT方法作为参数传递给Javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆