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
vfDeveloper.ax341vfDeveloper.ax341 

Pass data between components

Hi, Is that possible to pass data between components? Thanks.
Ron HessRon Hess
not sure i follow you, components have controllers, which produce data for the page.
I supose both components could call the same controller, but i believe this would be two seperate instances.

one component controller could call the getter inside another component controller (after creating an instance), i think..

can you describe a simple use case?
vfDeveloper.ax341vfDeveloper.ax341
I have one page and 3 components inside. All of them (page and components use the same controller). In controller i have property which decides which of the component to render. This property is being changed handling button click actions. But as you mentioned above(each component has its own instance of controller). So i need some way to have one controller instance for all components or pass data between controller instances in order to fulfill my task. Thanks
Ron HessRon Hess
ok, i think you can use a component argument, called < apex : attribute

Code:
 <apex:attribute name="type" type="String"
                description="Specify type of chart: line, pie, pie3"
                assignTo="{!charttype}" required="true" />

 

the main page decides which component to render, then the controler has a getter/setter for the value it would like to pass on,

the compnent has an attribute with an assignTo= , which connects to a setter in the component controller, when that component is rendered, it should have the attribute value set, so that the componen can see and assign it to it's own setter.

not sure i'm communicating this clearly...
vfDeveloper.ax341vfDeveloper.ax341
Is there way in visualforce components to handle button clicks(or other events) of child components?(Like for instance in ASP.NET: child component expose some event and page just subscribes to that event and handle it). This will definitely solve my problem.But i do not know how to implement it in visualforce. Thanks
dchasmandchasman
Ron's answer is correct about data passing (or binding) - component attributes support the Inversion of ontrol (IoC) or dependency injection design patterns where basically the component advertises its requirements via strongly typed attributes (almost any valid apex code data type is supported - including arrays). As you have already figured out you can inject the attribute's value into your controller via the apex:attribute's assignTo attribute. The doc should have made this clear - I'll see about making sure it does.

As to your second question w.r.t. event binding - are you looking for client side (in browser) or server side event handling or both?

For server side you can again use apex:attribute - this time using type="ApexPages.Action" as the type (NOTE: assignTo is not supported for this type yet) and then use the attribute inside your component to bind the passed in action method to any action attribute for any component used inside your component.

We currently do not have a client side event framework - it is something we're looking into - but you can leverage any javascript technology you choose inside VF pages and components. Ext.js and Dojo both have client side pub/sub event systems you could leverage for example.