Strange behaviour with ajaxupdate and portlet

Hi all,

I get a strange behaviour with my always-on-development-app.

In the home page I have a Portlet that shows items currently saved in shopping kart. In this portlet there is an Ajax link that calls erase kart action:




<?php echo CHtml::ajaxLink('Svuota carrello', array('shoppingkart/reset'), array('update'=>'#shoppingkartbox')); ?>



And here the action in ShoppingKartController:




 public function actionReset() {

        // Funzione per la cancellazione delle sessioni carrello ormai non più attive

        $model = ShoppingKart::model();

        $transaction = $model->dbConnection->beginTransaction();


        try {

            $shoppingkart = ShoppingKart::model()->currentKart()->deleteAllWithScopes();

            $transaction->commit();


        } catch (Exception $e) {

            $transaction->rollBack();

        }


        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

        if(Yii::app()->request->getIsAjaxRequest()) {

            $portlet = new ShoppingkartBox();

            $model->refresh();

            $portlet->model = $model->currentKart(Yii::app()->session->sessionId);

            $portlet->renderContent();            

        } else

            $this->redirect(isset(Yii::app()->user->returnUrl) ? Yii::app()->user->returnUrl : array('index'));

    }



In the home page I have also some other widget where there is an "add to basket" ajax link.


<?php echo CHtml::ajaxLink("Acquista", array('shoppingkart/add', 'productid' => $product->id), array('update'=>'#shoppingkartbox')); ?>

and following the corresponding action in ShoppingKartController class:




public function actionAdd($productid) {               


        if(isset($productid)) {


            $model = ShoppingKart::model()->getCurrentProductInKart($productid);


            if ($model->count() > 0) {

                $model = ShoppingKart::model()->getCurrentProductInKart($productid)->find();                

                $model->quantity++;                

            } else {

                $model = new ShoppingKart;

                $model->quantity = 1;

            }


            if (!Yii::app()->user->isGuest)

                $model->iduser = Yii::app()->user->id;


            $model->sessionid = Yii::app()->session->sessionId;

            $model->idproduct = $productid;            


            if($model->validate()) {


                $model->save();


                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

                if(Yii::app()->request->getIsAjaxRequest()) {

                    $portlet = new ShoppingkartBox();

                    $portlet->model = $model->currentKart(Yii::app()->session->sessionId);

                    $portlet->renderContent();

                } else

                    $this->redirect(isset(Yii::app()->user->returnUrl) ? Yii::app()->user->returnUrl : array('index'));

            }

        }

    }



Both ajaxLink and corresponding actions work correctly, but I haave a strange behaviour:

  • when I first call actionAdd, then with ajaLink I correcly add products to the shopping kart. Ajax reload Portlet and #shoppingkartbox is rendered correctly

  • if then I click on reset ajax link, the ajax link doesn’t call reset action, but call add action again

  • if I reload / refresh the page and then call reset ajax link, then ajax call reset action correctly.

Where am I wrong?!? :blink:

Ok, I’m really newbie on ajax and Yii, but I cannot figure out how this behaviour could happen. Any help appreciated :)

Mauro

Another input. I tried to set different IDs to both ajaxLinks, but now If i first call add action, then reset action won’t run. If I refresh page anche then click reset, corresponding action runs correctly.

Help :(

Hi! I can just suggest you to do "log" and see exactly what happen in you actions. Log is one of my best friends =).

Ciao Sensorario,

unfortunately log is not helpful.

Anyway, I found a workaround, but I have introduced now a new problem.

I solved modifying actionAdd in this way:




// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

                if(Yii::app()->request->getIsAjaxRequest()) {

                    $model->refresh();

                    echo $this->processOutput($this->widget('ShoppingkartBox', array('model'=>$model), true));

                } else

                    ...



But now I have another problem: when I click on ajaxLink contained in ShoppingKartBox portlet, the corresponding action (


<?php echo CHtml::ajaxLink('Svuota carrello', array('shoppingkart/reset'), array('update'=>'#ajaxshoppingkart')); ?>

)

runs x times, where x is the time of ajaxLink action add click.

Something is telling me that I’m doing it wrong… :angry: