5
<apex:component>
 <apex:attribute name="l" description="l" type="double" assignTo="{!Received_L}"/>
  <apex:attribute name="varLng" description="t" type="double" assignTo="{!Received_T}"/>  
   //***** from here i want to call assignLocation()--------

</apex:component>

My Controller for Component

   public class myController {
         public double Recieved_L{get;set;}
         public double Recieved_T{get;set;}

         public myController(){
                //do initializations
          } //constructor        

    public PageReference assignLocation() {
        l=Recieved_L;
        t=Recieved_T;
        return null;
    }

I dont wanna call this method in constructor, I want to call myMethod() after page load. (Better if I am able to do this without using JavaScript/can also suggest)

2
  • 2
    I'm voting to close this question as off-topic because I am not working on this anymore. May 16, 2016 at 8:07
  • Nilesh Mendhe. This is not off-topic question. If you are still want an answer then I can help you with this..
    – Ratan Paul
    May 16, 2016 at 8:51

3 Answers 3

4

I don't know what you want to do after page is fully loaded so I will assume that you want to call a method to calculate a value using Received_T and Received_L.

You can use apex:actionFunction to call your calculation method. But you will need Javascript to run the method automatically after the page is loaded.

For example:

This is your page.

<apex:page>
    <c:mycomponent l="20" varLng="30" />
</apex:page>

This is your controller. In myMethod(), you calculate the value of calc_result.

 public class myControllerComponent {
 public double Received_L{get;set;}
 public double Received_T{get;set;}
 public double calc_result{get;set;}

public void myMethod() {
    this.calc_result = double.ValueOf(Apexpages.currentPage().getParameters().get('Received_T')) * double.ValueOf(Apexpages.currentPage().getParameters().get('Received_L'));
}      
}

In your component, you call javascript function when the page is loaded and pass both values to myMethod(). And then rerender div1 to see that calc_result shows up as 600.

<apex:component controller="myControllerComponent">
<script>
    onload = function() {
        myMethod('{!Received_T}', '{!Received_L}');
    }
</script>
<apex:attribute name="l" description="l" type="double" assignTo="{!Received_L}"/>
<apex:attribute name="varLng" description="t" type="double" assignTo="{!Received_T}"/> 
<apex:pageBlock title="My Custom Component">
    <p>
    <code>L</code> is "{!Received_L}"
    <br/>
    <code>T</code> is "{!Received_T}" <br/>
  </p>
  <apex:outputPanel id="div1">Result is "{!calc_result}"</apex:outputPanel >
</apex:pageBlock> 
<apex:form >
<apex:actionFunction name="myMethod" action="{!myMethod}" rerender="div1">
    <apex:param name="Received_T" value="" />
    <apex:param name="Received_L" value="" />
</apex:actionFunction>
</apex:form>
</apex:component>
1

The initialization of components is poorly designed. Because params get assigned through setters the constructor is basically useless, as you can't perform any logic on your input params.

It would make much more sense if all Component controllers implemented an interface with a postAssignment() event function that would get called after your attributes were all assigned. (But I regress...)

Here's a way you can hack it that isn't horrible:

1:

Add a boolean property called something like init and have it call your initialization function

global with sharing class MyComponentController {
    public Account myAccount {get; set;}
    public Boolean init{
        get{
            init();
            return false;
        }
    }

    public void init(){
       //put all your initlization logic here.
       //your assigned properties will be set when this executes
       myAccount.name = 'Hello World';
    }
}

2:

In your apex component use add the init property in any <apex:> tag to the TOP of your component. This will cause it to fire your init() function before the getters of all other properties.

<apex:component controller="MyComponentController">
    <apex:attribute name="account"
      type="Account"
      assignTo="{!myAccount}"
      required="true"
      description="" />

    <apex:outputText rendered='{!init}' />
    <apex:outputField value="{!myAccount.name}" />
</apex:component>
1
0

Any reason a Command Button won't work?

2
  • I want to execute this right after page loads and every time its refreshed so Command button wont be good choice Aug 5, 2014 at 6:18
  • I'd use JS, and a promises Library like Kris Koval's Q to fire this up whenever, but not resolve the promise until all the data is present.
    – Kevin P
    Aug 5, 2014 at 19:39

You must log in to answer this question.

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