1

At the time I still have not received any feedback about my previous question so I went ahead with a different approach to my problem. Bare with me part of my problem might be because of how I changed my approach along the way.

Basically I have two custom components

  1. Widget (which uses Widget.cls as its controller)
  2. Series of metric components (which uses {Type}Controller.cls as their controllers

Previously I tried to use the Widget as a subcomponent of each metric component so that I could reuse the attributes and other elements. Based on the lack of answers on my previous post I don't think that is possible. So instead, I now use the Widget component on the VFP and then it uses one of the metric components based on the widget-type attribute given. For example:

<c:Widget data-controller="{!dashboard}" widget-type="Chart-Comparison" widget-title="Some Title">

The widget then contains all the attributes that each metric component would need and it passes those by passing the instance of itself like so:

<c:MetricType widget="{!this}" rendered="{!IF(Component=='chart',TRUE,FALSE)}" />

This works great, now each metric component just contains the following line:

<!-- Widget Instance Attributes -->
<apex:attribute name="widget" assignTo="{!component}" description="Widget component controller used" 
                type="Widget" required="true"/>

Now the problem begins... The metric component controller extends the Widget.cls which worked fine before but now even though I can still execute all the shared widget methods, all of the instance variables are missing (not set). So although they were set when the Widget was constructed, by the time my {MetricController}.cls gets constructed the Widget instance it has is empty making it useless.

I've tried to pass the instance into the controller like so:

public class MetricController extends Widget {

    //========= SETUP GETTERS/SETTERS ============//
    /* Although we are extending the Widget's methods, the widget instance data
    is passed through this component variable and therefore needs to be used to setup 
    all instance data */

    public Widget component { get; 
        set {
            component = value;
            setupWidgetInstance();
        } 
    }  
}

Problem is although I can set any given variable with something like:

void setupWidgetInstance(){
    params = component.params;
}

it seems I would need to set do that for every variable on each of these controllers and that is not going to work. I've killed many hours today narrowing this problem down to this and could really use some help to bring it home (also so I can go home :P #deadlines)

1 Answer 1

2

The Widget and MetricController controller instance are independent objects not the same object which is why the extends Widget doesn't work.

Your composition approach looks like a good way to go. If the widget (probably needs a better name) just holds attributes you can just grab them as you need them in the rest of the controller; there is no need to copy the values into other variables:

public Widget w {get; set;}

    Integer n = w.params.size();
    ...
    String message = 'Hello ' + w.name;
    ...
1
  • I'm marking your answer for 2 reasons: 1) I don't like having open ended questions. 2) Essentially I'm came to the same conclusion which led me to create a shared object which I felt was best referred to as FluxCapacitor given its nature of storing data between these two objects. A brief summary of how I implemented it can be seen here: gist.github.com/Xtremefaith/46735947b0df20db53d4 Commented Mar 10, 2016 at 21:11

You must log in to answer this question.

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