Primefaces ajax callback with parameter to javascript

Viewed 14310

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.

1 Answers
Related