Tuesday, January 28, 2014

Explain ActionFunction, ActionSupport and ActionPoller in Visualforce

Explain ActionFunction, ActionSupport and ActionPoller in Visualforce

apex:ActionFunction : This component helps to envoke AJAX request (Call Controllers method) directly from Javascript method. It must be child of apex:form. Read here – www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm
<apex:actionFunction name="sendEmail" action="{!sendEmailFunction}"></apex:actionFunction>

apex:ActionSupport : This component adds Ajax request to any other Visualforce component. Example : Commandlink button has inbuilt AJAX functionality however few components like OutputPanel does not have inbuilt AJAX capabilities. So with the help of this component, we can enable AJAX. Read more here.
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"  action="{!incrementCounter}"  rerender="counter" status="counterStatus"/>
</apex:outputpanel>

apex:ActionPoller : This is timer component which can send AJAX request on pre-defined interval. Minimum interval is 5 sec and default is 60 sec.
<apex:actionPoller action="{!incrementCounter}" rerender="counter" status="counterStatus" interval="50" />

Similarities:
Both action support and function can be used to call a controller method using an AJAX request.

Differences:
1. Action function can call the controller method from java script.
2. Action support adds AJAX support to another visualforce component and then call the controller method.
    for example:
     <apex:outputpanel id="outptpnl">
             <apex:outputText value="click here"/>
         <apex:actionSupport event="onclick" action="{!controllerMethodName}"  rerender="pgblck" />
     </apex:outputpanel>   
Here action support adds AJAX to output panel, so once you click on output panel controller method will be called.
3. Action function cannot add AJAX support to another component. But from a particular component which has AJAX support(onclick, onblur etc) action function can be called to call the controller method.
Example:
 <apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myactionfun" />
In this example onlick of input checkbox "myactionfun" action function is called from where controller method "actionFunMethod" gets called.

Apart from this, the main difference between the "two" action support and action function is that, the action function can also be called from java script.
Example:
<apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myJavaMethod()" />
<script>
   function myJavaMethod(){
     myactionfun();// this call the action function
  }
  </script>



No comments:

Post a Comment