Newer Version Available
Deferred Creation of Dynamic Components
The Apex method that defines a dynamic component is by default executed at
page load time, before any action method that’s defined for
the page is run. Set the invokeAfterAction attribute of a dynamic component to true to wait for page actions to be completed before the
method that creates the dynamic component runs. This enables you to
design dynamic components that change depending on the result of,
for example, a page initialization action or a callout.
Here’s a page that has a single dynamic component, which
is created after the page’s action method, pageActionUpdateMessage, is completed.
1<apex:page controller="DeferredDynamicComponentController"
2 action="{!pageActionUpdateMessage}" showHeader="false">
3
4 <apex:dynamicComponent componentValue="{!dynamicComp}" invokeAfterAction="true"/>
5
6</apex:page>Here’s the associated controller that provides the dynamic
component definition, and illustrates the effect of the invokeAfterAction attribute.
With the default behavior for dynamic components, the msgText value that’s set in
the constructor is displayed by the dynamic component. Setting invokeAfterAction="true" on the dynamic
component changes that behavior. The page waits for the pageActionUpdateMethod to be completed
and then creates the dynamic component, and so the component
displays the value for msgText that’s set in the pageActionUpdateMessage action method instead.
1public class DeferredDynamicComponentController {
2
3 private String msgText { get; set; }
4
5 public DeferredDynamicComponentController() {
6 this.msgText = 'The controller is constructed.';
7 }
8
9 public Component.Apex.OutputPanel getDynamicComp() {
10
11 // This is the component to return
12 Component.Apex.OutputPanel dynOutPanel= new Component.Apex.OutputPanel();
13 dynOutPanel.layout = 'block';
14
15 // Child component to hold the message text
16 Component.Apex.OutputText msgOutput = new Component.Apex.OutputText();
17 msgOutput.value = this.msgText;
18 dynOutPanel.childComponents.add(msgOutput);
19
20 return dynOutPanel;
21 }
22
23 public Object pageActionUpdateMessage() {
24 this.msgText= 'The page action method has been run.';
25 return null;
26 }
27}Deferred Creation of Dynamic Components and Other Actions
invokeAfterAction="true" affects dynamic components immediately at page load time, because
that’s when page actions run. Setting invokeAfterAction="true" reverses
the order of component creation and any action method on the
page. That is, the order of execution is changed for action methods on all of the following
components.
- <apex:actionFunction>
- <apex:actionPoller>
- <apex:actionSupport>
- <apex:commandButton>
- <apex:commandLink>
- <apex:page>
- <apex:togglePanel>
When invokeAfterAction="false" is set on a dynamic component, the order of execution is as follows.
This is the default behavior for dynamic components.
- Invoke the dynamic component’s creation method, which constructs the component.
- Invoke the action method.
- Rerender the page.
When invokeAfterAction="true" is set on a dynamic component, the order of execution is as follows.
- Invoke the action method.
- Invoke the dynamic component’s creation method, which constructs the component.
- Rerender the page.