添加脚本包括一个顶点:组件

Adding script includes with an apex:component?

本文关键字:组件 顶点 脚本 包括一 添加      更新时间:2023-09-26

我希望通过apex:component调用我的脚本。所以在我的页面上没有 3 个包含,我想要

<c:scripts />   

"脚本"组件将包含以下内容。

<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>

但是,如果我只想要这些脚本之一,我可以将参数传递到组件调用中吗?像下面这样...

    <c:scripts
      myScript = true />

然后组件...

<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>

有没有一种优雅的方法可以做到这一点?

干杯

找到答案:)

<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>

然后在我的页面中:

<apex:page>
    ...
    <c:Scripts includejQuery="true" />
    ...
</apex:page>