Ajax And Yiiframework

Did you do combination of Ajax and Yii? Write here your experiences and help another users.

I already work with ajaxLinks but it sometimes work and sometimes not. Problem for me was when I had more the same objects and I have the same ajaxLink-with special ID and it doesn’t work perfectly.

Right now I am start with developing video likes and dislikes. So firstly I want to listen more experienced users and developers how you are doing ajax updates.

Thank you guys

Ok so I have the same problem now. I used ajaxLink and when I have more links on page it mix after few clicks.

So for example I have two images of videos on one page. I am using CListView with following itemView




<a href="<?php echo $data->getVideoUrl(); ?>">

    <img src="http://img.youtube.com/vi/<?php echo $data->video->youtube; ?>/mqdefault.jpg" width="220" />

</a>

<div id="video-<?php echo $data->video->id; ?>">

   <?php echo $data->video->countLike(); ?>

   <?php 

        echo CHtml::ajaxLink(

            'Like',          // the link body (it will NOT be HTML-encoded.)

            array('like/video','object'=>$data->video->id,'answer'=>  LikeStatus::LIKE), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

            array(

                    'update'=>'#video-'.$data->video->id

            )

        );

    ?> 

    <?php echo $data->video->countDislike(); ?>

    <?php 

        echo CHtml::ajaxLink(

            'Dislike',          // the link body (it will NOT be HTML-encoded.)

            array('like/video','object'=>$data->video->id,'answer'=>  LikeStatus::DISLIKE), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

            array(

                    'update'=>'#video-'.$data->video->id

            )

        );

    ?> 

</div>



Function in Like controller is following (I am just testing so I just echo result)




public function actionVideo($object,$answer)

    {

        if($answer==LikeStatus::LIKE) LikeVideo::addLike ($object);

        else LikeVideo::addDislike ($object);

        $model=Video::model()->findByPk($object);

        

        echo $model->countLike().' '.CHtml::ajaxLink(

                            'Like',          // the link body (it will NOT be HTML-encoded.)

                            array('like/video','object'=>$model->id,'answer'=>  LikeStatus::LIKE), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

                            array(

                                    'update'=>'#video-'.$model->id

                            )

                        ).' '.$model->countDislike().' '.CHtml::ajaxLink(

                            'Dislike',          // the link body (it will NOT be HTML-encoded.)

                            array('like/video','object'=>$model->id,'answer'=>  LikeStatus::DISLIKE), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

                            array(

                                    'update'=>'#video-'.$model->id

                            )

                        );

    }



And what is wrong?

For example:

I click on like on first video - worked

I click on dislike on fist video - worked

I click on dislike on second video - worked

I click on like on first video - worked

I click on like on second video - and it changed like on first video MIX STARTED

Why? Do you have experience with this?