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
BryanHartBryanHart 

Apex controller property not being passed properly into a custom compont's componentBody

I have a custom component that sits within a repeater.

the custom component's attribute is set via the repeater.

the custom component's attribute uses assignTo to set a property on its controller.

 

if (and only if) I use that attribute in the body of another custom component (inside the first custom component), then the attribute only sets the first time the repeater creates the component, never thereafter. I can access the visualforce attribute as expected... but the property on the controller is never set again.

 

I have a simple code example to illustrate the problem.

Note that the two case IDs ({!case.id} and {!myCase.id}) should be the same.

 

 

Any idea what is wrong?

 

*EDIT*

looking into it more...

the controller is being set properly, but the wrong value is being passed into the component body for some reason.

Its like {!myCase} only gets resolved once and gets reused for every repetition.

 

 

 

TestPage

<apex:page standardController="Contact">
 <apex:repeat value="{!Contact.Cases}" var="c">
   <div><c:TestComponent case="{!c}" /></div>
 </apex:repeat>
</apex:page>

 

 

TestComponent

<apex:component controller="TestComponentController">
    <apex:attribute name="case" type="Case" description="TODO" assignTo="{!myCase}"/>

    <c:TestComponent2>these should be the same: {!case.id}, {!myCase.id} </c:TestComponent2>
</apex:component>

 

TestComponentController

public class TestComponentController
{
    public Case myCase {get;set;}
}

 TestComponent2

<apex:component>
      <apex:componentbody/>
</apex:component>

 

BryanHartBryanHart

 

Found a workaround.

Load the controller property into an apex:variable and reference that from within the component body.

 

In the example I gave, modify the TestComponent like this:

<apex:component controller="TestComponentController">
    <apex:attribute name="case" type="Case" description="TODO" assignTo="{!myCase}"/>

    <apex:variable var="myCaseId" value="{!myCase.id}"/>
    <c:TestComponent2> these should be the same: {!case.id}, {!myCaseId} </c:TestComponent2>
</apex:component>

 

I raised a bug, but its just a P3 so it'll probably never get looked at. There was a similar bug from 2009 which was likewise still open.
This workaround seems to do the trick, though