2016-03-30 20 views
0

比方说,我有一个名为testComp的组件,它具有字符串属性testProperty。对应testComp.java,是一个js文件testComp.js挂毯:在相应的JavaScript文件中访问页面/组件属性

如何在testComp.js中访问testProp?

我尝试了以下,但它给出了一个错误。

console.log(${testProp}); 

我知道我可以在tml文件中做$ {testProp},但是我需要在javascript文件中访问这个属性。我已经在邮件列表中搜索,但迄今没有运气。任何想法如何完成?

回答

1

试试这个:

testComp.java

@Inject 
private JavaScriptSupport javaScriptSupport; 

@AfterRender 
private void after() throws Exception { 
    JSONObject arguments = new JSONObject(); 
    arguments.put("testProperty", this.testProperty); 
    javaScriptSupport.addInitializerCall("testComp", arguments); 
} 

testComp.js

Tapestry.Initializer.testComp = function (json) { 
    new testComp(json); 
}; 


function testComp(json){ 
    alert(json.testProperty); 
}