0

I am loading some comments in via ajax, default is 5 onload then the user can request more if they want.

This is more a question of good practice.

Where is the best place to build to html elements that will be displayed on the page.

Should I build them up in the javascript file then display them?

I am using Yii framework if this is any help.

1
  • Just append the html created in javascript to a layer in your page Nov 4, 2015 at 11:57

1 Answer 1

0

There are certainly a number of ways you can achieve this. I've faced a similar issue before (also related to a Comments system) and what I did was to create a custom Widget and simply load it in the controller. Widgets can have view files which will be loaded upon request and you can simply inject the output into a specific place in your page. Here's what I have in my code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.