2017-02-09 60 views
2

我有以下JavaScript方法:如何在java方法中访问'this'?

myFunc = function(callback) { callback.call(this, "hello", "world"); } 

和I'm通过实现该“呼叫”的方法的Java对象。在java调用方法中,我得到了两个参数“hello”和“world”,但不是“this”(当然)。有没有办法从Java访问“this”?

我用d3.js和d3连接java有很多回调,'this'是d3存储选择的地方。

感谢

+0

你能请使用callback()方法提供Java类的代码示例。 – Vladimir

+0

我没有设法在此评论中添加代码示例,因此我将添加作为以下问题的答案... –

回答

0

我可不是真正的编码在Java中,但JRuby的。为了创建一个Java示例 我将不得不简化我的代码。也许这可以帮助一些。如果没有, 我会尝试做一个Java示例。

# Function f1 will call the callback methods cb1 and cb2 with 'this' variable 
# This is just a notation for creating javascript function. It calls 
# @browser.executeJavaScriptAndReturnValue(scrpt), whith the function 
# body (everything between EOT) modified to make a valid javascript script. 
# f1 is a function with 2 arguments cb1, and cb2 which should be the 
# callback functions 
f1 = B.function(<<-EOT) 
    (cb1, cb2) { 
     cb1.call(this, "banana", "milk"); 
     cb2.call(this, "arroz", "feijao"); 
    } 
EOT 

# Proc is a closure. It receives two arguments |food1, food2|. This will 
# become a java object per JRuby´s magic 
proc = Proc.new { |food1, food2| puts "I hate #{food1} and #{food2}" } 

# now call method f1 passing proc as the first argument and the block as 
# the second argument. So cb1 = proc and cb2 = <the block bellow>. Method 
# 'send' grabs the given arguments, converts them to java objects and then 
# calls jxBrowser 'invoke' method with the given arguments. 
f1.send(proc) { |food1, food2| puts "eu gosto de #{food1} e #{food2}" } 

这段代码执行的结果是:

I hate banana and milk 
eu gosto de arroz e feijao 

可以看出,在“这个”变量只是走了。我想能够 捕获“这'以某种方式变量,以便能够使用块中的上下文。我设法制作了一个解决方法,可以捕获'this'变量,但它需要将该块封装在另一个JavaScript函数中。

此代码的整体思想是允许JRuby开发人员编写Ruby代码并在jxBrowser中执行此代码,而无需使用任何JavaScript。通过下载mdarray-sol GEM已经可以看到这方面的例子,或者去https://github.com/rbotafogo/mdarray-sol。在那里你可以看到JRuby使用d3.js的多个例子。

+0

在您的代码示例中,我没有看到使用call()方法注册Java对象的位置使用JxBrowser JavaScript Java Bridge API:https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript – Vladimir

+0

我想你没有理解我的问题。我确实注册了一切正常,代码正在工作。但是,如果我有一个javascript函数,如:myFunc = function(callback){callback.call(this,'Hello Java));}我按照javascript:myFunc(window.java)的建议做,然后在JavaObject 'call'方法我无法读取'this'变量 –

+0

“this”将被转换为JSValue,所以你的call()java方法应该看起来像call(JSValue theThis,String message)。 – Vladimir

0

请确保您按照指示在https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript,并与call()方法正确地注入你的Java对象:

Java代码:

browser.addScriptContextListener(new ScriptContextAdapter() { 
    @Override 
    public void onScriptContextCreated(ScriptContextEvent event) { 
     Browser browser = event.getBrowser(); 
     JSValue window = browser.executeJavaScriptAndReturnValue("window"); 
     window.asObject().setProperty("java", new JavaObject()); 
    } 
}); 
... 
public static class JavaObject { 
    public void call(JSValue window, String message) { 
     System.out.println(message); 
    } 
} 

JavaScript代码:

window.java.call(window, 'Hello Java!');