ajaxLink update problem

Hello guys, I’m fairly new to Yii(i like the framework very much) and I’m having problems using ajaxLink recently…

[color="#FF0000"]views/pages/_lifestreams.php:[/color]




<?php foreach($lifestreams as $lifestream): ?>

<div class="post">

    <ul>

        <li id="ls-<?php echo $lifestream->author->id.'-'.$lifestream->id; ?>">

			<div class="content">

				<?php

                	$this->beginWidget('CMarkdown', array('purifyOutput'=>true));

                	echo $lifestream->content;

                	$this->endWidget();

                ?>

			</div>

            

            <span><?php echo CHtml::encode(DateTimeAgo::effectTime('posted',$lifestream->create_time)); ?></span>

            

            <div class="meta">

                <div id="fav-<?php echo $lifestream->id; ?>">

                    <?php if(!$lifestream->isFavorite()): ?>

                    <?php echo CHtml::ajaxLink (

                                CHtml::encode('Favorite'),

                                CController::createUrl('lifestream/favorite', array('fav'=>$lifestream->id)), 

                                array('update' => '.fav-'.$lifestream->id,),

                                array('title'=>'Mark as Favorite','class'=>'fav-'.$lifestream->id)); ?>

                    <?php else: ?>

                    <?php echo CHtml::ajaxLink (

                                CHtml::encode('Remove Favorite'),

                                CController::createUrl('lifestream/unfavorite', array('unfav'=>$lifestream->id)), 

                                array('update' => '.unfav-'.$lifestream->id,),

                                array('title'=>'Remove Favorite','class'=>'unfav-'.$lifestream->id)); ?>

                    <?php endif; ?>

                </div>    

            </div>        		

        </li>

    </ul>

</div><!-- end post -->

<?php endforeach; ?>



[color="#FF0000"]models/Lifestream.php:[/color]




public function isFavorite()

    {

        $favorite = Lifestream_Favorite::model()->find(array('user_id=:userID','lifestream_id=:lifestreamID'),

            array(':userID' => Yii::app()->user->id, ':lifestreamID' => $lifestream->id));

            

        if($favorite===null)

            return false;

        else

            return true;

            

    }



[color="#FF0000"]controllers/LifestreamController.php:[/color]




       /**

	 * Favorite

	 * 

	 */

	public function actionFavorite()

	{

		$favorite = new Lifestream_Favorite;

		

		$favorite->user_id = Yii::app()->user->id;

        $favorite->lifestream_id = $_GET[fav]; 

		$favorite->save();  

	}

    

       /**

	 * Unfavorite

	 * 

	 */

	public function actionUnfavorite()

	{

		$favorite =   Lifestream_Favorite::model()->find(array('user_id=:userID','lifestream_id=:lifestreamID'),

            array(':userID' => Yii::app()->user->id, ':lifestreamID' => $_GET[unfav]));

        

        $favorite->delete();   

	}



here is how it looks like by default:

when i click favorite on the latest post, it goes like this:

and then when i refresh the page, it looks like this:

every "favorite" link text gets changed even though i only clicked on one, where did I go wrong? … and also, the text is suppose to change when i click it but it instead goes hidden. why is that?

You’ll need a different approach. Regarding the “hidden” link: since you don’t return anything from the controller, the ‘fav-’ div will become empty after the update.

It’s not obvious to me why all texts would change, you seem to have unique id’s in all relevant places. Try $this->id instead of $lifestream->id in isFavorite().

/Tommy

Here is what I did:

  1. added views/pages/__favorites

<?php if(!$lifestream->isFavorite()): ?>

                    <?php echo CHtml::ajaxLink (

                                CHtml::encode('Favorite'),

                                CController::createUrl('lifestream/favorite', array('fav'=>$lifestream->id)), 

                                array('update' => '.fav-'.$lifestream->id,),

                                array('title'=>'Mark as Favorite','class'=>'fav-'.$lifestream->id)); ?>

                    <?php else: ?>

                    <?php echo CHtml::ajaxLink (

                                CHtml::encode('Remove Favorite'),

                                CController::createUrl('lifestream/unfavorite', array('unfav'=>$lifestream->id)), 

                                array('update' => '.unfav-'.$lifestream->id,),

                                array('title'=>'Remove Favorite','class'=>'unfav-'.$lifestream->id)); ?>

                    <?php endif; ?>



  1. removed code found at #1 from views/pages/_lifestreams then added a renderPartial linking to the #1 view file

  2. added


$this->renderPartial('/pages/__favorites');

on both favorite and unfavorite actions found in LifestreamController.php …

  1. on isFavorite(), I replaced $lifestream->id with $this->id …

… it still doesn’t work…

Any suggestions or solutions to this? … how about an alternative implementation? … like json, etc…

I can’t seem to figure this out yet >.<

can someone suggest a different approach to solving this problem?.. :(

Probably you should try leaving both links in the document and make them (their div’s) hidden/visible on ajax ‘success’.

(not tested)

/Tommy