2014-12-31 50 views
0

我正在構建Joomla 3.x的組件,並且遇到處理ajax請求的問題。 源是可用在Github:JomCity在Joomla 3.x組件的視圖中的Ajax

我的請求被從venues視圖製成:/com_jomcity/site/views/venues/tmpl/default.php

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    jQuery('#main_search_request').keyup(function() { 
     var request_content = jQuery(this).val(); 
     jQuery.get(
     // Specify the server URL and the $_GET datas to send 
     'index.php?option=com_jomcity&task=searchVenues&format=json', 
     { search: request_content }, 
     // The server will respond with some data encoded in JSON 
     function(data){ 
      jQuery('#results').html(''); 
      for (var i = 0; i < data.venues.length; i++) { 
      var venue = data.venues[i]; 
      console.log(venue); 
      jQuery('#results').append(
       '<div class="grid-item">' + 
       '<a href="#">' + 
        '<img src="http://placekitten.com/g/80/80" alt="">' + 
        '<div>' + 
        '<div>' + 
         '<h3>'+venue.name+'</h3>' + 
         '<span>* * * * *</span>' + 
        '</div>' + 
        '<div>Short descrption</div>' + 
        '</div>' + 
       '</a>' + 
       '</div>' 
      ); 
      } 
     } 
    ); 
    }); 
    }); 
</script> 

這AJAX腳本與組分(現場部分)的主控制器進行通信,所以我想知道如何從模型com_jomcity/site/models/venues.php獲得數值。

這段代碼可能嗎?或者我應該在視圖的控制器中處理ajax請求(我沒有找到如何做到這一點)?

+0

? – Toretto

+0

這個問題/答案可能會闡明你想如何接近它:https://joomla.stackexchange.com/questions/146/what-is-the-proper-way-to-make-an-ajax-call -in-組件/ 2541#2541 – GDP

回答

0

感謝@Torreto和@GDP的參與。

我終於繼續在主控制器上直接創建一個與數據庫交換的任務,所以沒有使用任何模型。我認爲它應該存在一個更好,更清潔的方式來編寫這個Ajax調用,所以我願意接受任何進一步改進的建議。

我用,如果它可以幫助別人的代碼:

/com_jomcity/site/views/venues/tmpl/default.php

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    jQuery('#main_search_request').keyup(function() { 
     var request_content = jQuery(this).val(); 
     jQuery.get(
     // Specify the server URL and the $_GET datas to send 
     'index.php?option=com_jomcity&task=searchVenues&format=json', 
     { search: request_content }, 
     // The server will respond with some data encoded in JSON 
     function(data){ 
      // Clean the #result div between two search results 
      jQuery('#results').html(''); 
      // Read and display each objects returned in the json response 
      for (var i = 0; i < data.length; i++) { 
      var venue = data[i]; 
      jQuery('#results').append(
       '<div class="grid-item">' + 
       '<a href="#">' + 
        '<img src="http://placekitten.com/g/80/80" alt="">' + 
        '<div>' + 
        '<div>' + 
         '<h3>'+venue.en_name+'</h3>' + 
         '<span>* * * * *</span>' + 
        '</div>' + 
        '<div>Short descrption</div>' + 
        '</div>' + 
       '</a>' + 
       '</div>' 
      ); 
      } 
     } 
    ); 
    }); 
    }); 
</script> 

/com_jomcity/site/controller.php

class JomCityController extends JControllerLegacy 
{ 
    /** 
    * Search in all the venues 
    * @return string The message to be displayed to the user 
    */ 
    public function searchVenues() { 
     if (isset($_GET['search'])) { 
      // Get a db connection 
      $db = JFactory::getDBO(); 
      // Create a new query object 
      $query = $db->getQuery(true); 
      // Select all records from the jomcity_venues_items table 
      // Where the name or the description correspond to the user's search keywords 
      // Order it by the en_score field 
      $query 
       ->select('*') 
       ->from('#__jomcity_venues_items') 
       ->where('`local_name` like "%'.$_GET['search'].'%" OR `en_name` like "%'.$_GET['search'].'%" OR `description` like "%'.$_GET['search'].'%"') 
       ->order('en_name ASC'); 
      // Reset the query using our newly populated query object 
      $db->setQuery((string)$query); 
      // Load the results as a list of stdClass objects. 
      $this->venues = $db->loadObjectList(); 
     } 
     echo json_encode($this->venues); 
    } 
} 
要獲得從模型中的價值