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?!?
Ok, I’m really newbie on ajax and Yii, but I cannot figure out how this behaviour could happen. Any help appreciated
Mauro