Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call my jQuery function after Primefaces script is finished

Description:

When using Primefaces html page is rendered by Servlet. Sometimes Servlet generates only html tags, but sometimes generates javascript.

Case 1

XHTML:

<h:outputText id="outputId" value="Hello!!!" />

Generated:

<span id="outputId">Hello!!!</span>

In this case it is very easy to add addtional callback using jQuery script. Beacouse all html elements are already generated.

Case 2:

XHTML:

<p:schedule id="bigCalendarId" value="#{csController.lazyEventModel} view="month" />

This example shows that not always pure html is generated. In this case javascipt is generated:

<script id="formId:bigCalendarId_s" type="text/javascript">$(function(){PrimeFaces.cw('Schedule','widget_formId_bigCalendarId',{id:'formId:bigCalendarId',defaultView:'month',offset:3600000,header:{left:'prev,next today',center:'title',right:'month,agendaWeek,agendaDay'}},'schedule');});</script>

So Primefaces use some script to finish initialization. This function is called when DOM tree is built.

Question:

Primafaces uses script to build view, but I would like to add some calbacks to generated content. Problem is that my script has to be executed after javascript generated by Primefaces is finished. How to achieve that? How to execute my script after Primefaces?

like image 806
Hubert Avatar asked Aug 14 '26 19:08

Hubert


1 Answers

There are 2 ways that work for me, when I want to execute a JS function after page load in Primefaces:

  • either use jQuery (as it is already used by Primefaces), best solution is to nest document.ready twice, so that my JS code is executed after all Primefaces code:
    • jQuery(document).ready(function () { jQuery(document).ready(function () { // twice in document.ready to execute after Primefaces callbacks myInitialisationFunction(); }); });
  • or use p:remoteCommand with autorun, and place oncomplete JS callback on it
    • this way form is submitted by AJAX to server but no listener executed on server side, a JS callback is executed afterwards
    • <p:remoteCommand oncomplete=" myInitialisationFunction(); " autoRun="true"/>
like image 75
OndroMih Avatar answered Aug 16 '26 08:08

OndroMih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!