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
hemmhemm 

Including another VF page using the same controller as master

I currently have apex:include tags in a VF page that dynamically insert other pages into the master page.  The pages being inserted have the same Controller as the master page.  My main reason for doing this is organization so I do not have one huge page.  I am keeping some JavaScript in the included pages and that JavaSCript uses values in the controller to operate.

 

Below is a dumbed down representative sample of the code.   In my main page, I will re-render the dynamicJavaScript panel to execute code.

 

 

<apex:outputPanel id="dynamicJavaScript">
        
  <apex:include pageName="js_Script1"  id="js_Script1" rendered="{!mode='x'}"/>

  <apex:include pageName="js_Script2"  id="js_Script2" rendered="{!mode='y'}"/>

</apex:outputPanel>

 

My main questions have to do with View State and efficiency.  

 

  • Is there a downside to doing things this way?  
  • Do multiple controllers get invoked (one from each page) that all take up memory? Or does VF figure it out to run under 1 controller.
If needed, I could pull all the JavaScript into the main page, but it's nice to do it this way from a development standpoint.

 

 

 

bob_buzzardbob_buzzard

Its difficult to say if there's a downside to this without knowing your design in details.  I assume you've considered components and composition and found reasons not to go those routes.  There's some good information about when to use composition vs components vs page includes in chapter 13 of the VisualForce Developer's Guide.

 

Its my understanding that each page will have its own instance of a controller, but having been through the developer's guide I can't find a clear statement either way. 

 

 

hemmhemm

Understood.  The included page can definitely access data from the main page's controller just by having the same controller defined on the included page.  I have many places on my page that could interact with the affected by the included page and I've had trouble communicating between controller and component controller before because, I believe, you can only pass primitives in and have to recreate objects and class structures.

 

When I look at the View State editor, I see multiple controllers there, but the total KB taken up is the same if I hack it all together into one big page.  However, I am questioning whether there is a UI issue on the View State inspector under this scenario and it's mis-reporting the view state size.  Just a theory based upon some other observations.  

 

I guess I was hoping for Salesforce to come along here and say that the included page will leverage the existing controller if one is already running and not invoke a new one.  That appears to be the case, but wanted to be sure.

 

I haven't looked into composition yet.  I thought that was just for Site Templates, but will look further.

bob_buzzardbob_buzzard

Having read through the examples in the VF Developer's Guide I'd say you are correct about the sharing of the controllers if one is already instantiated.  In the example the included (child) and parent pages both use the same controller.  The child page contains a form that updates a property in the controller.  The parent page is then accessing this field.

 

I guess the view state may be showing multiple references to the same controller instance.

 

BTW You can pass objects and arrays and others to components.  For example I've passed the controller save method down to a component that needed to save changes.

 

However, if you aren't reusing them across multiple pages, there's no added value to components.

 

 

hemmhemm

Thanks!

 

Yeah, right after posting that, I re-read the Components piece and saw the thing about objects.  I was pleasantly surprised about that.