Struggling with sending a Primefaces bean property to JavaScript directly in order to open a new page and write the content of the bean property into it.
The version of Primefaces being used is 4.0.
A command button that is being utilized looks like this:
<p:commandButton id="buttonId" update="buttonId, otherComponentId"
value="press me" process="@this" actionListener="#{homeController.myMethod}"
oncomplete="handleComplete(xhr, status, args)">
</p:commandButton>
In the handleComplete javascript function, 'args' is undefined while 'xhr' and 'status' are not. The definition of the javascript function is as follows:
function handleComplete(xhr, status, args){
var w = window.open();
w.document.open();
alert(xhr.responseText.substring(100));
alert(status);
alert(args);
var d = '<head></head><body>'+ args.firstParam +'<body>';
w.document.write(d);
w.document.close();
}
The first alert displays the page correctly, the second one results in a parse error, showing: Uncaught TypeError: Cannot read property 'firstParam' of undefined
The intention is to pass a string like this through 'args':
public String MyMethod() {
RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("firstParam", "my string");
return "";
}
and be able to access it in JavaScript using args.firstParam
.
The method gets called (confirmed with working printscreens).
This approach needs to be tried instead of setting the text into a
<h:outputText id="myText" escape="false" rendered="true"
value="#{homeController.property}" />
and then extracting the innerHTML of this element since what is obtained with innerHTML might not match the string variable in the bean. This method does work but not entirely as desired. Curious about why the args object is undefined or any other way to manage the bean property from JavaScript effectively. Thank you.