2015-05-04 70 views
1

我正在使用Joomla 3.x中的自定義構建模塊,它使用Ajax來顯示一些實時數據。在由Ajax調用執行的函數中,我需要訪問模塊參數,我不知道如何執行此操作。通常情況下,我可以在模塊PHP文件中放入$ var = $ params('paramName','default')來獲取參數,但在Ajax調用時不可用。這是我的模板代碼執行Ajax調用:Joomla 3.x在Ajax調用中獲取模塊參數

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    jQuery.get('index.php?option=com_ajax&module=whatsinport&method=getWhatsInPort&format=json', function(data) { 
     console.log(data); 
     var response = jQuery.parseJSON(data); 

這是我helper.php類代碼:

class modWhatsInPortHelper { 

    public static function getWhatsInPortAjax() 
    { 
    $results = array(); 
    $results['status'] = 'ok'; 

    $app = JFactory::getApplication(); 
    $serverName = $app->getCfg('mod_whatsinport_serverName'); 
    $dbName = $app->getCfg('mod_whatsinport_dbName'); 
    $dbUser = $app->getCfg('mod_whatsinport_dbUser'); 
    $dbPwd = $app->getCfg('mod_whatsinport_dbPwd'); 

    $connectionInfo = array("Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPwd, "ReturnDatesAsStrings"=>true); 
    $conn = sqlsrv_connect($serverName, $connectionInfo); 

的$ APP-> getCfg似乎並沒有做我想做的 - 我猜它只用於Joomla配置設置。我也試過:

$app = JFactory::getApplication(); 
    $params = $app->getParams(); 
    $serverName = $params->get('mod_whatsinport_serverName'); 
    $dbName = $params->get('mod_whatsinport_dbName'); 
    $dbUser = $params->get('mod_whatsinport_dbUser'); 
    $dbPwd = $params->get('mod_whatsinport_dbPwd'); 

但是這也沒有工作。我忘了包含我的模塊配置文件:

<?xml version="1.0" encoding="utf-8"?> 
<extension type="module" version="3.1.0" client="site" method="upgrade"> 
    <name>WhatsInPort</name> 
    <author>Chris Krohn</author> 
    <version>1.0.0</version> 
    <description>Displays a list of current vessels in port.</description> 
    <files> 
     <filename>mod_whatsinport.xml</filename> 
     <filename module="mod_whatsinport">mod_whatsinport.php</filename> 
     <filename>index.html</filename> 
     <filename>helper.php</filename> 
     <filename>tmpl/default.php</filename> 
     <filename>tmpl/index.html</filename> 
    </files> 
    <config> 
     <fields name="params"> 
     <fieldset name="basic"> 
      <field name="mod_whatsinport_serverName" type="text" default="" label="Server Name" description="NETBIOS name of the database server to connect to." /> 
      <field name="mod_whatsinport_dbName" type="text" default="" label="Database Name" description="Name of the database with the HMLOG table." /> 
      <field name="mod_whatsinport_dbUser" type="text" default="" label="Database User" description="User name to login to the server with." /> 
      <field name="mod_whatsinport_dbPwd" type="password" default="" label="Database Password" description="Password to login to the server with." /> 
     </fieldset> 
     </fields> 
    </config> 
</extension> 

回答

0

想通了。應該是:

$app = JFactory::getApplication(); 
    $module = JModuleHelper::getModule('mod_whatsinport','WhatsInPort'); 
    $params = new JRegistry($module->params);  
    $serverName = $params->get('mod_whatsinport_serverName'); 
    $dbName = $params->get('mod_whatsinport_dbName'); 
    $dbUser = $params->get('mod_whatsinport_dbUser'); 
    $dbPwd = $params->get('mod_whatsinport_dbPwd');