Push Javascript from server to client using Primefaces?

UI Components for JSF
Post Reply
colm
Posts: 11
Joined: 05 Sep 2009, 00:43

09 Sep 2009, 02:12

Hi,

I am wondering if there is an API in Primefaces to push some javascript to the client during a server side call, which is then queued and evaluated on page load??

I am looking for simular functionality as IceFaces JavascriptContext.addJavascriptCall( 'blah' );

I have tried using "oncomplete", but unfortunately (for me) the oncomplete javascript is parsed "onsubmit" prior to completeing the request, so it makes it hard to render the javascript dynamically. For example, if the server state is responsible for determining the javascript state....

I have also tried to "repaint" a form (which contains new javascript) but the DOM does not seem to recognise the new script and continues evaluating the old javascript...

My application requirements are to do something after a form submission in javascript, based on the server response... I.E if (true) do this... else do that... where TRUE is dynamic from server..

To achieve this, I have also tried to dynamically write out a javascript function "onSearchReturn()" inside the form ''flightOptionForm" which is repainted after a partial submit....

Even though the function is rewritten, the page DOM does not seem to recognise the new javascript in memory??

Code: Select all

<p:commandLink id="searchAction" onclick="if (!validate()) return false;" value="Search Flights" action="#{aggregationSearchOptions.returnOptions}" update="flightOptionForm,totalForm,flightOptionsForm_Return,flightOptionsForm_Departing,bestOptions" styleClass="search-flights button" oncomplete="onSearchReturn()" />

Code: Select all

<script type="text/javascript">
               /* <![CDATA[ */
        function onSearchReturn(){
        if(#{aggregationSearchOptions.errorMessages}){#{requestScope['validationJavascript']}}else{toggleModifySearch(null);}
        }
        /* ]]> */        
    </script>
Any help or guidance is appreciated.

Colm

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

09 Sep 2009, 20:04

Hi Colm, I think it's possible with a trick. PrimeFaces PPR can execute any script in the partial response so following might work although I'vent tried it. Assuming you're using facelets.

Code: Select all

<p:outputPanel id="script">
    #{bean.myscript}
</p:outputPanel>

<p:commandButton update="script" action="#{bean.click}" />

Code: Select all

public String click() {
   myscript = "<script ...>alert(''hi");</script>"
}
On server side you can programmatically change the myscript variable, it'll be evaluated after response. oncomplete is the better way if the script executed is same and does not depend on a logic. I'm planning to add an easier way to implement this nowadays but I think the solution I've described would work.

colm
Posts: 11
Joined: 05 Sep 2009, 00:43

10 Sep 2009, 01:18

Great, Thank you.

This works perfectally. I had previously tried to render the Javascript inside a h:outputText, but the JSF reference Impl kept writing the JS outside the generated <Span> - it is obviously stricter on what script content you could output.

Regards

Colm

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

10 Sep 2009, 02:07

Glad it works, outputPanel is useful for cases like this.

ashdam
Posts: 23
Joined: 16 Oct 2010, 21:21

19 Oct 2010, 12:06

my outputpanel prints it as normal text :(
Solved:

You have to write <script>#{mybean.myscript}</script> or will just show text

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

20 Oct 2010, 11:21

We're planning to add this feature in a future release, probably 2.3

http://code.google.com/p/primefaces/iss ... il?id=1342

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: chieddy and 7 guests