我正在通过ajax加载一些注释,默认为5 onload然后用户可以根据需要请求更多。
这更是一个良好做法的问题。
构建将在页面上显示的html元素的最佳位置在哪里。
我应该在javascript文件中构建它们然后显示它们吗?
如果有任何帮助,我正在使用Yii框架。
答案 0 :(得分:0)
肯定有很多方法可以实现这一目标。我之前遇到过类似的问题(也与评论系统有关),我所做的就是创建一个自定义Widget并将其加载到控制器中。窗口小部件可以包含视图文件,这些文件将根据请求加载,您只需将输出注入页面中的特定位置即可。 这是我的代码中的内容:
/protected/widgets/LoadComments.php
class LoadComments extends CWidget {
public $offset;
public $limit;
public $comments;
public function run() {
//(some offset and limit logic here)
$criteria = new CDbCriteria();
// some criteria definitions here (...)
$this->comments = Comment::model()->findAll($criteria);
$this->render('listComments', array());
}
}
/protected/controllers/CommentController.php
public function actionListComments($offset = 0, $limit = 10) {
$this->widget("application.widgets.LoadComments", array(
"offset" => $offset,
"limit" => $limit,
));
}
/protected/views/comments.php
<div id="comments"></div>
<script>
$(document).ready(function(){
$.ajax({
type: 'GET',
data: { offset : 0, limit : 10},
url: '<?php echo Yii::app()->createUrl('/comment/listComments');?>',
success: function(data) {
$('#comments').html(data);
}
});
});
</script>