Reply
Contributor
dji973
Posts: 3
0

Dynamic Component Problem with TypeMethods

Hello everyone,

 

I created this post because I have a problem. I want to create a dynamic component to add it to a page. So here is my configuration :

 

TabLayoutLead

<apex:page id="tabLayout" >

...

     <c:myDynamicComponent compToAdd="QuoteComponent" contAssoc="QuoteComponentController" extName="ClassExtensionName"/>

...

</apex:page>

 

 

MyDynamicComponent

<apex:component controller="MyDynamicController" >

  <apex:attribute name="compToAdd" description="The component to dynamically add to the stage"   type="String" required="true" assignTo="{!compToAdd}" />
  <apex:attribute name="contAssoc" description="The controller associated to the component"   type="String" required="true" assignTo="{!contAssoc}" />
  <apex:attribute name="extName" description="The extension label associated to the compoonent to get the regular or custom component" type="String" required="true" assignTo="{!extName}" />

  <apex:dynamicComponent id="theParent" rendered="false" componentValue="{!addComponent}"/>

</apex:component>



MyDynamicController

global class MyDynamicController {

  public Type t { get; set; }
  public String compToAdd { get; set; }
  public String contAssoc { get; set; }
  public String extName { get; set; }

  public MyDynamicController()

  {


  }


  public ApexPages.Component getAddComponent()

  {

    if (extName != 'Default')
      compToAdd = compToAdd + extName;
    t = Type.forName(compToAdd);
    return (ApexPages.Component) t.newInstance();
  }
}

 

My final goal is, having a list of custom components, to be able to create them on my page by just calling one single line. So in MyDynamicComponent, I create the new component using the apex:dynamicComponent. But I have a problem, because in my controller, the function called dynamicComponent.getAddComponent(), when creating a newInstance using TypeMethods, throws a NullPointerException because  t = Type.forName(compToAdd) returns null. I've figured out that TypeMethods are for Classes only and not Components. Then I wanted to create a Class which extends ApexPages.Component so I can use it with the TypeMethods but I then got the following error "Error: Compile Error: Parent class has no 0-argument constructor for implicit construction".

So I wanted to know, can I technically do what I want to do ? I come from a Flex/Java world so maybe I'm at the limit of Salesforce.

If I can do it, do you have any suggestions for me please, maybe you already did this.

 

Thank you very much.

Super Contributor
sfdcfox
Posts: 4,041
0

Re: Dynamic Component Problem with TypeMethods

You can create a wrapper class/Interface. Here's one possible solution:

 

// Just need 1 of these.
global Interface ComponentReference { ApexPages.Component getInstance(); }

 

// Need 1 per component.
public class MyComponent1Reference implements DynamicComponentReference { ApexPages.Component getInstance() { return new Component.mynamespace.MyComponent1(); } }
public void dosomething() {
  Type t = Type.forName('mynamespace','MyComponent1Reference');
  ApexPages.Component cr = t.newInstance().getInstance();
}

The value of this is fairly limited in scope; you need a way to configure elements, and because of the nature of binding, this might blow up later.

 

Your Flex background has served you well; regrettably, Apex Code is significantly more restricted in terms of dynamic types than Flex is.

~ sfdcfox ~


I am a sandwich. That is all.

Contributor
dji973
Posts: 3
0

Re: Dynamic Component Problem with TypeMethods

Thanks for the quick reply. I'll try this and I let you know
Contributor
dji973
Posts: 3
0

Re: Dynamic Component Problem with TypeMethods

Thanks. Your solution works very well. To be a little bit more tricky, what I did is that in my controller I added this getInstance function so I dont need to create any more classes.

 

Now I have one more question. Apparently, the dynamicComponent is added to the Page between between controller's constructor and variable assignment. Which means that, in my dynamicComponent adding, I cannot used fields and attributes that I need to customize my component.

 

Do you know any way to get these attributes before the dynamicComponent's componentValue is called please ?

 

Thank you.

Super Contributor
sfdcfox
Posts: 4,041
0

Re: Dynamic Component Problem with TypeMethods

Getters are called after setters, so you should be able to see the values and attributes you have. Just remember to not set any variables in a getter method, and do not depend on any values being available in setter methods. If you're still having problems, maybe you could post an example?

~ sfdcfox ~


I am a sandwich. That is all.