Reputation: 2662
I use Apache Tapestry as web application framework.
I have variable in my java code. I need value of this variable in javascript after page loaded.
For example java class:
@Import(library = "RoomManagement.js")
public final class RoomManagement{
@Property
private long contactId;
}
and js in RoomManagement.js:
window.onload = function(){
alert(contactId);
}
How can I pass it directly to javascript?
I can not pass value to js like to template, cause it is .js file not .tml.
I can add invisible tag to my page, write value to this tag and read it from js. But do you know another way?
Upvotes: 2
Views: 1486
Reputation: 3893
You will need to use the JavaScriptSupport
service.
Your java file:
@Import(library = "RoomManagement.js")
public final class RoomManagement{
@Inject
private JavaScriptSupport javascriptSupport;
@Property
private long contactId;
@AfterRender
private void setup() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("contactId", contactId);
javascriptSupport.addInitializerCall("RoomManagement",jsonObject);
}
}
Your RoomManagement.js
Tapestry.Initializer.RoomManagement = function (parameters) {
//do whatever you need here
alert('Your contactId: ' + parameters.contactId);
};
Upvotes: 3