3

I have a number of components that all share the same controller but have slightly different layouts.

Everything's working great but for part of it to work in trying to find a way to tell the controller which component it is that's calling it?

If it were a regular page i would just use ApexPages.currentPage().getUrl(). But I can't seem to find a way to of anything similar with a component?

I even tried to just create a parameter like this in the component

<apex:attribute name="page"  description="This is the page." type="String" assignTo="{!pageName}"/>

where i would pass the page name along with the controller call, like so

 <c:PrintBankFees page="PrintBankFees" />

but that doesnt seem to work either?

in my controller i have (simplified greatly to focus on the issue in question)

public String pageName {get;set;}
public String instructions {get;set;}

public PrintController() {
        instructions = pageName;
}

and my component is setup like

<apex:component controller="PrintController">

     <apex:attribute name="page"  description="This is the page." type="String" assignTo="{!pageName}"/>
     <apex:outputText escape="false" Value="{!instructions}" />

</apex:component>

but instructions is always blank?

1 Answer 1

3

I think (hard to tell since you've simplified the controller) but this is due to you trying to use the value inside of the constructor of the controller.

The setters for controller members are not called until after the constructor has run. A pattern I typically use is make sure that all getter methods make sure initialisation code has been run like so (simplified example):

public String pageName {get;set;}
public String instructions {set;}
private Boolean initialised = false;

public PrintController() {
    // don't use values from parameters here, they won't be set!
}

private void Init() {
    if(initialised)
        return;

    instructions = pageName;
    initialised = true;
}

public GetInstructions() {
    Init();

    return instructions;
}
2
  • interesting, i'll give this a shot. The bigger question tho, do you know an easier way to get the name of the component into the controller? Sep 30, 2014 at 4:33
  • Not really, I think easiest would be just passing it through where you use it. You can create a setup where the component controller and main controller inherit from the same base class and hook them up with a reference to each other, but it's probably overkill for this scenario. Will dig out a link for you!
    – Matt Lacey
    Sep 30, 2014 at 4:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .