Question

I'm looking to call my scripts through an apex:component. So instead of having 3 includes on my page, I want

<c:scripts />   

and the 'scripts' component will contain the following.

<apex:component >       
 <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-1.9.1.js')}"/> 
 <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-ui-1.10.3.custom.min.js') }" /> 
 <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/anotherScript.js') }" />        
</apex:component>

But if I only want one of these scripts, can I pass a argument into the component call? Something like the following...

    <c:scripts
      myScript = true />

And then the component...

<apex:component >       
 Boolean myScript = <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-1.9.1.js')}"/> 
 <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-ui-1.10.3.custom.min.js') }" /> 
 <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/anotherScript.js') }" />        
</apex:component>

Is there an elegant way of doing this?

Cheers

Was it helpful?

Solution

Found an answer :)

<apex:component>

    <apex:attribute name="includejQuery" type="Boolean" required="false" default="false" description="True to include jQuery" />

    <apex:outputPanel layout="none" rendered="{!includejQuery}">
        <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-1.9.1.js')}"/> 
    </apex:outputPanel>

        <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/jquery-ui-1.10.3.custom.min.js') }" /> 
        <apex:includeScript value="{!URLFOR($Resource.jQuery, 'jQuery/js/anotherScript.js') }" />

</apex:component>

Then in my page:

<apex:page>
    ...
    <c:Scripts includejQuery="true" />
    ...
</apex:page>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top