1
votes

I am trying to make a component in visualForce who apply some treatement on any List that I pass by parameter :

<apex:component controller="TheController">

<!-- Attribute -->
<apex:attribute name="list_Of_Objects"
    description="Received varaible of data type string"
    assignTo="{!listAllObjects}" type="MyObjects[]"
    required="true" />

    {Code of the component}
</apex:component>   

This is how I call my Component :

<apex:page Controller="ComponentController">

    <c:TheComponent list_Of_Objects="{!objects}" />

</apex:page>

My component should display the information on the list, but the parameter passed to my parameter are initialized after that the constructor of ComponentController was called, so I cant call any other method to do the treatement on the arrays (passed in parameter)

One solution consist to make a button on the visualForce page that call the specific method when its clicked, but its not what I want.

I wish to know if there is any way to 'auto-call' a method in ComponentController class, without using JavaScript (just VisualForce or apex)

1

1 Answers

2
votes

The (simplified) order of execution for component controllers is:

  • constructor
  • component's parameters (assignTo's)
  • getters as required by visualforce

In constructor all params are blank, not passed yet. You have access to whatever was in the URL of main page (ApexPages.currentPage()) and that's about it.

So the trick is to have your "decorators" run not in constructor but in a helper getter method

public class ComponentController{

    public List<MyOBjects> listAllObjects {get; set;}

    public ComponentController(){}

    public List<SomethingDecorated> getBetterObjects(){
        List<SomethingDecorated> betterObjects = doYourMagic(listAllObjects);
        return betterObjects;
    }
}

And then in component simply reference {!betterObjects}.