Reputation: 405
I am struggling to send a Primefaces bean property to javascript directly in order to open a new page and write the content of the bean property into the new page.
I am using Primefaces 4.0.
I have a command button 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 handleComplete javascript function args is undefined, but not xhr and status. javascript function definition:
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();
}
first alert it's giving me the page, the second one parse error, and the error is: Uncaught TypeError: Cannot read property 'firstParam' of undefined
I want to pass in the args a string like this:
public String MyMethod() {
RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("firstParam", "my string");
return "";
}
and access it in javascript with args.firstParam
.
the method is called, (I have some printscreens that work.)
I have to try this way and not to set the text into a
<h:outputText id="myText" escape="false" rendered="true"
value="#{homeController.property}" />
and then get innerHTML of this element because what I will get with innerHTML will not be the same as the string variable in the bean. This method works but not as I would like. I am wondering why the args object is undefined or how else could I get the bean property manageable from javascript. Thank you.
Upvotes: 4
Views: 15361
Reputation: 10058
First thing you should make sure that your method is called. Put some logging or debug your method if it's called correctly.
If not you might want to add the process attribute into the button, and set the process into @this.
If in this stage your method is called, then you have a validation error in your page.
And I would call my methods in actionListener not in action, and put the () in the end. Differences between action and actionListener
Try the following
<p:commandButton id="buttonId"
value="press me" actionListener="#{homeController.myMethod()}"
process="@this"
oncomplete="handleComplete(xhr, status, args)">
</p:commandButton>
Since your error is "firstParam" is undefined and not the "args" is undefined, you might want to make sure that your firstParam value is not null !.
Upvotes: 6