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
DSLDSL 

custom component updating main controller?

Hi,

 

If I create a custom component, is there any way to update a value on the parent controller? Let's say as a parameter passed into the custom component, I gave a value like {!value}. Can the controller behind the custom component modify that variable that's on the main page's custom controller? In java you might do this with a shared bean or session variable. What about in apex?

 

I have several VF tabs on my page. Each tab is a different business function. I end up re-using code on multiple VF pages so it would be great to make each tab a component so I can re-use thm elsewhere.  Right now it's monolithic. But a link on one tab may cause the VF page to jump you to another tab and use a commandlink parameter.

 

So if we were to break this up into custom components, we would need to somehow pass data between them. Is that possible somehow? It looks like variables only flow one way or maybe we just don't know how to do it?

 

Any thoughts?

 

Thank you,

DSL

Best Answer chosen by Admin (Salesforce Developers) 
DevAngelDevAngel

You can definitely do this.  What you need to do is establish a kind of handshake between the two controllers.

 

The solution is outlined in this wiki article Controller Component Communication

 

 

Here is a link to a page that implements the technique described in the article referenced above.

 

http://ls-developer-edition.na6.force.com/MyPage


 

Cheers 

 

 

 

 

 

 

 

 

 

 

Message Edited by DevAngel on 07-28-2009 04:16 PM
Message Edited by DevAngel on 07-28-2009 04:28 PM

All Answers

iceberg4uiceberg4u
Custom components are VF pages having there own controllers.You can use attribute tag to pass in the values and generate html if you want from the controller itself.
DSLDSL

Does anyone know a way to have the data go back the other way? From the component back into the controller? Or else to call a method in the controller? Not as a new object, but as the existing controller which already may have certain variables set.

 

This would be like managed beans in java.

 

Thanks!

Luke@TWSLuke@TWS

I'm having problems with this too. I need to create a component which adds functionallity to a field. To get and set the value of the field in the VF page to which my component is added I need the value to be passed in as an attribute. This works fine and allows me to update the value of the field. I then need to use the value of the field in my component in a webservice request so I have to use assignTo to get the value into my components controller. The field in my component then needs to reference the value in the components controller so I can change it and I can't then get the value back to the VF page.

 

The only way I know of is to pass in the VF page controller is as an attribute. Would this help in your case? Doesn't in mine :(

 

To do it:

 

add to the component controller:

 

public MyController ThePageController {get;set;}

 

add to the component:

 

<apex:attribute name="ThePageController" type="MyController" description="The main page controller" assignTo="{!ThePageController}"/>

 

in the VF page:

 

<apex: page controller="MyController">

 

<c:MyComponent ThePageController="{!Controller}" />

 

in apex file "MyController":

 

    public MyController getController() {
        return this;
    }

 

Message Edited by Luke@TWS on 04-17-2009 04:08 AM
JPClarkJPClark

Was there ever a solution found for this?

It seems useless to have a component manipulate data, without being able to use the changes.

Thanks

DevAngelDevAngel

You can definitely do this.  What you need to do is establish a kind of handshake between the two controllers.

 

The solution is outlined in this wiki article Controller Component Communication

 

 

Here is a link to a page that implements the technique described in the article referenced above.

 

http://ls-developer-edition.na6.force.com/MyPage


 

Cheers 

 

 

 

 

 

 

 

 

 

 

Message Edited by DevAngel on 07-28-2009 04:16 PM
Message Edited by DevAngel on 07-28-2009 04:28 PM
This was selected as the best answer
thoban2thoban2

When I read your posting, I thought this might answer a problem I need to solve.  But, again, I think I hit a dead end.

 

I need to do something similar.  But rather than use the component in a VF Page, I need to use the component in a VF Email Template. However, the email template does not have a controller associated with it.  Instead, it holds an sObject that is passed in the "relatedToType" attribute.  If I include a component in a VF EMail Template that uses some custom controller that I write, how can I access the "relatedToType" sObject in the custom controller used by the custom component?

ForceTechieForceTechie

Hi,

 

Can anybody tell me why virtual class was used in that (http://ls-developer-edition.na6.force.com/MyPage) example. Also, can't we use a single global class which might work as common class between Component controller and Page controller?

Richard CorfieldRichard Corfield
I've just had to solve this kind of problem, but I solved it by passing a mutable object between the page and the component. The object is passed by reference, so shared by both controllers.

This solution is a lot simpler and seems to work. It also constrains the communication to that which is supported by the object that is passed, which reduces coupling in the system. A future iteration of my code will have multiple components, all of which can communicate through instances of my mutable object.

I think this may answer ForceTechie's question above.

The current issue I'm having is unit testing, as I need to gain access to my component's controller in the unit test. I may have to look into mocking parts of my system out. The child component in my case is a dynamic component, determined at runtime.