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
The KnightThe Knight 

How to pass values between two components

In a VF page I have two  <apex:component> A and B. This components are not related with each other each having their own controllers. How can I pass data between these two components? The same way I need to pass data from these components to the main page.which is again having a different controller. 

  

  I have seen the example of dependency Injection mechanism. But it helps in passing values from page to

components or from parent to child components. But I need to pass values between two components with no relation.

 

Any help would be appreciated

Joseph

Best Answer chosen by Admin (Salesforce Developers) 
DevAngelDevAngel

Hi The Knight,

 

I suppose on the components you could create actionFunctions that interact with the controller.  ActionFunctions result in a Javascript callable method with parameter support.  The rendered javascript is in the Global "namespace" AFAIK.

 

So, to pass a value from component a to component b you could define in component b an actionFunction:

 

 

<apex:actionFunction name="gimmeA_Value" action="{!takeAValue}" >
<apex:param name="a_value" value="" />

</apex:actionFunction>

 

Then, in component a you could simply call the gimmeA_Value javascript function when handling the appropriate event:

 

 

<script>
function handleEvent() {
gimmeA_Value("valuable value");
}
</script>

 

 Thoughts?

 

 

Cheers

 

 

 

All Answers

DevAngelDevAngel

Hi The Knight,

 

I suppose on the components you could create actionFunctions that interact with the controller.  ActionFunctions result in a Javascript callable method with parameter support.  The rendered javascript is in the Global "namespace" AFAIK.

 

So, to pass a value from component a to component b you could define in component b an actionFunction:

 

 

<apex:actionFunction name="gimmeA_Value" action="{!takeAValue}" >
<apex:param name="a_value" value="" />

</apex:actionFunction>

 

Then, in component a you could simply call the gimmeA_Value javascript function when handling the appropriate event:

 

 

<script>
function handleEvent() {
gimmeA_Value("valuable value");
}
</script>

 

 Thoughts?

 

 

Cheers

 

 

 

This was selected as the best answer
NeeharikaNeeharika

Hi Dave,

 

Tried your solution. But the value is not being passed from one controller to other. any other solution please?

George NguyenGeorge Nguyen
What Dave has suggested is half of the work. That is, you can access the values between the two components via Javascript.

If you then want to pass those values to your controller; use Javascript Remoting or another actionFuction on your main page and/or the other component that also takes parameters.

hope this helps.