function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luke Vang 52Luke Vang 52 

Controller loading after javascript is called in reRender of apex component.

Hi,

Here's the situation, I am trying to dynamically show some custom apex components. I have an parent outputpanel with some nested outputpanel children. The outputpanel gets reRendered from an action function the the nested outpanels get rendered depending on some value. Everything looks good and is working, but each apex component has a visualforce remoting function which is supposed to be called when the component loads. When the component loads, the js remoting function tries to fire but it says the controller can't be found. It looks like the remoting function is trying to fire before the actual controller loads. If I look in the debug logs, the constructor for the controller is being called.

Apex Page:
<apex:form >
    	<apex:actionFunction name="setName" action="{!onChangeName}" reRender="containerPanel">
    		<apex:param name="name" assignTo="{!name}" value="" />
    	</apex:actionFunction>
    </apex:form>

<apex:outputPanel id="containerPanel" >
                             <apex:outputPanel rendered="{!name == 'Bob'}">
                             <c:Component1 a_name="{!chartName}"  />
                             </apex:outputPanel>
                              <apex:outputPanel rendered="{!name == 'Barb}">
                              	  <c:Component2 name="{!name}"/>
                             </apex:outputPanel>
   </apex:outputPanel>
 
<script>
        const j$ = jQuery.noConflict();
        j$(document).ready(function() {
//call action function
 setName("Bob");
});
</script>

Component:
 
<apex:component controller="Controller">

<apex:attribute name="name" type="String" description="name" assignTo="{!name}" />

<div id="{!name}" class="container"></div>

  <script>

var name = {!name}
//call remoting function when component is loaded
        Visualforce.remoting.Manager.invokeAction("{!$RemoteAction.Controller.genName}",
                                                  name,
                                                  function(result, event) {
                                                      if (event.status) {
//do something
}
});

</script>
</apex:component>

Errors:
Controller not found for: 
Unable to invoke action .....  no controller and/or function found
Luke Vang 52Luke Vang 52
Sorry I always wanted to add.... adding a javascript timer function before the remoting function is called makes the remoting function work. So there looks to be some sort of delay of when the remote function fires and the controller being loaded in the component.