从JavaScript到Java(一个GWT故事) [英] From javascript to java (A GWT story)

查看:92
本文介绍了从JavaScript到Java(一个GWT故事)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在为GWT设计一个嵌入式网页浏览器(SketchUp)的应用程序。我可以通过将window.location值更改为skp :: myFunciton @ myParams来控制Sketchup。 Sketchup可以在浏览器中执行JavaScript。我想要做的就是要求sketchup给我模型的内容。

  public static native void getModel()/ * -  {
$ wnd.location =skp:getModel @;
} - * /;

第二次绘制结果后。但是,我们如何将它恢复到gwt?问题是入口点实例启动了请求,JSNI只能将静态方法映射到javascript。



我想我有一个事件和元素的解决方案...

  // Sketchup javascript 
var gwtwidget = document.getElementById(myTextArea)
gwtwidget.value =blahblah ;
gwtwidget.onchange();

然后监听GWT的变化。唉,它不起作用。 Gwt自己的事件系统覆盖,吸收,阻止(或其他)事件。我应该采取什么方法?我已经通过网络搜索信息,但我当然不能把我的头转过来。我猜测答案是...

<1>从javascript调用入口点实例方法(以某种方式)

2从JavaScript中触发事件这将被gwt拾起(不知何故)

3设置一个异步回调接口机制的排序(以某种方式)

解决方案

接口应该很简单。

例如,假设我们在GWT中有一些对象。假设我们有一些JS接受一些回调参数的函数。所以在gwt中我们会有这样的东西:

  public static native void executeFunctionWithCallBack(MyCallback callback)/ *  -  {
var callBackWrapper = function(param){
callback。@ com.package.MyObject :: onSuccess(*)(param);
}
$ wnd.invokeFunctionWithCallback(callbackWrapper)
} - * /;

如果您想调用实例方法,您不仅需要公开方法,还需要公开实例也应该被调用。例如。您必须将实例作为参数传递给JSNI方法(或以其他方式从JSNI获取它)。然后你创建一个JS函数,它将在实例上调用方法。就这样。没有更多的魔力=)

So, I'm designing an app in GWT for an embedded web browser (Sketchup). I can control Sketchup by changing the window.location value to "skp::myFunciton@myParams". Sketchup can execute javascript in the browser. What I want to do is ask sketchup to give me the contents of its model.

public static native void getModel() /*-{
    $wnd.location = "skp:getModel@";
}-*/;

After a second sketchup has a result. But how do we get it back to gwt? The problem is the entrypoint instance launched the request and JSNI can only map static methods to javascript.

I thought I had a solution with events and elements...

//Sketchup javascript
var gwtwidget = document.getElementById("myTextArea")
gwtwidget.value = "blahblah";
gwtwidget.onchange();

and then listening for the change in GWT. Alas, it doesn't work. Gwt's own event system overrides, sinks, deters (or whatever) the event. What approach should I take? I've been through the web in search of info but I certainly can't get my head round it. I'm guessing the answer is either...

1 Call an entrypoint instance method from javascript (somehow)
2 Fire an event from javascript that will be picked up by gwt (somehow)
3 Setup an async callback interface mechanism of sorts (somehow)

解决方案

Interface should be pretty simple.

For example, let's say we have some object in GWT. Let's say we have some function in JS which accept some callback as parameter. So in gwt we will have something like this:

    public static native void executeFunctionWithCallBack(MyCallback callback)/*-{
           var callBackWrapper =function(param) {
              callback.@com.package.MyObject::onSuccess(*)(param);
           } 
           $wnd.invokeFunctionWithCallback(callbackWrapper)  
     }-*/;

If you want to call instance methods, you need to expose not only method, but instance on which it should be called as well. E.g. you have to pass an instance as parameter to the JSNI method (or obtain it from JSNI in some other way). Then you create a JS function which will invoke method on instance. That's all. No more magic=)

这篇关于从JavaScript到Java(一个GWT故事)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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