Find your content:

Search form

You are here

Getters and setters order in a visualforce component

 
Share

I have below apex page which uses a custom component

<apex:page >
    <c:compo value="hello"></c:compo>
</apex:page>

Component is:

<apex:component controller="compocontroller">
    <apex:attribute name="value" type="string" description="tu" assignTo="{!valueclassvar}"/>
</apex:component>

Component Controller class is :

public class compocontroller {

    public  string valueclassvar { 
        get {
            system.debug('getmethod '+valueclassvar); 
            return valueclassvar;
        }
        set { 
            system.debug('setmethodfirst'+ valueclassvar);
            valueclassvar = value;
            system.debug('setmethod'+ valueclassvar);
        }
    }

    public compocontroller() {
        system.debug('constructor' + valueclassvar);
    }
}

When i execute the page,,and check debug log i can see

07:28:54.050 (50550965)|USER_DEBUG|[4]|DEBUG|getmethod null
07:28:54.050 (50561692)|SYSTEM_METHOD_EXIT|[4]|System.debug(ANY)
07:28:54.050 (50572663)|METHOD_EXIT|[7]|01pi0000005trqA|compocontroller.__sfdc_valueclassvar()
07:28:54.050 (50586810)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
07:28:54.050 (50595306)|USER_DEBUG|[7]|DEBUG|constructornull
07:28:54.050 (50601192)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
07:28:54.050 (50615792)|CODE_UNIT_FINISHED|compocontroller <init>
07:28:54.050 (50775094)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
07:28:54.050 (50784120)|SYSTEM_MODE_ENTER|true
07:28:54.050 (50800173)|CODE_UNIT_STARTED|[EXTERNAL]|compocontroller set(valueclassvar,hello)
07:28:54.050 (50884027)|SYSTEM_METHOD_ENTRY|[4]|System.debug(ANY)
07:28:54.050 (50900069)|USER_DEBUG|[4]|DEBUG|setmethodfirstnull
07:28:54.050 (50906953)|SYSTEM_METHOD_EXIT|[4]|System.debug(ANY)
07:28:54.050 (50924161)|SYSTEM_METHOD_ENTRY|[4]|System.debug(ANY)
07:28:54.050 (50931797)|USER_DEBUG|[4]|DEBUG|setmethodhello

Why is getmethod getting executed before constructor?I thought as per order of execution its the constructors that gets executed first,followed by expression/attributes of component..am i missing anything>


Attribution to: sfdc99999

Possible Suggestion/Solution #1

Your constructor is getting called first. Look at your constructor, it references valueclassvar. At this point, it actually calls the getter for valueclassvar, writes the system debug in the valueclassvar getter, and then returns to the constructor after which it calls System.debug in the constructor.

Furthermore, your setter will be called after the constructor is called. The setter is invoked by the assignTo attribute in your element in the component. This tends to lead to much confusion with components, and often means writing any dependent logic in your setters or elsewhere.


Attribution to: James Loghry
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34684

My Block Status

My Block Content