我正在为Joomla 3.x构建一个组件,我遇到了处理ajax请求的问题。 源代码可在Github上找到:JomCity
我的请求来自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 :(得分: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);
}
}